I got 2 models: Project and User. In Project:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
"users"=>array(self::MANY_MANY, 'User',
'projects_users(project_id, user_id)'),
);
}
I want to list all users who are in actual project, $model contain actual project:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->users,
'columns'=>array(
'ID',
'username',
'displayname',
'firstname',
'lastname',
'email',
/*
'password',
'isAdmin',
*/
array(
'class'=>'CButtonColumn',
'template'=>'{delete}',
),
),
)); ?>
Unfortuletlly I get an error:
Fatal error: Call to a member function getData() on a non-object in /var/www/vhosts/aevers.com/editor/framework/zii/widgets/CBaseListView.php on line 107
That's because $model->users is not a CActiveDataProvider : it's an array of CActiveRecords.
Try to use a CArrayDataProvider instead :
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=> new CArrayDataProvider($model->users),
[...]
),
)); ?>
U can Group And display the results according to Projects and users in cgridView
Try This Thread Grouping members
Related
I have following code in my controller:
public function actionAdmin()
{
$model=new MForm('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['ChManageForm']))
$model->attributes=$_GET['ChManageForm'];
$this->render('admin',array(
'model'=>$model,
));
}
and
const member=1;
const district=2;
My view(called admin)
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'ch-manage-form-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'form_name',
'region',
'phone_number',
'email',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
1 and 2 appears in the column called name(in my view file) and values of this column are saved in integer format. I need to show(in my view(admin)) member and district instead of numbers(e.g member instead of 1). How can I do it?
You can also specify the grid view column like this (doesn't require a helper in your model):
array(
'name'=>'form_name',
'value'=>'$data->form_name ? \'Member\':\'District\'',
'type'=>'text',
),
this works great if you only have 2 value in the variable.
Visit this URL to get more details.
http://www.yiiframework.com/forum/index.php/topic/14845-if-condition-inside-cgridview/
EDIT:
IF you have more than 2 values,
Model which you use to CGridView:
public function getValueText() {
return $this->getValueTextOptions[$this->form_name];
}
public function getValueTextOptions() {
return array(
1 => 'Member',
2 => 'District',
);
}
View with CGridView:
array(
'name'=>'form_name',
'value'=>'$data->getValueText()',
)
I want to fetch data from my table using CActiveDataProvider in Yii. Everything seems to be working well but when I want to display the data from another related table using relations, I get an error. 'Undefined variable $data'.
here is my admin.php view:
<h1>Manage Teams</h1>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'team-grid',
'dataProvider'=>$dataProvider,
'columns'=>array(
'id',
'team_name',
array(
'name'=>'league_id',
'type'=>'raw',
'value'=>$data->league->league_name,
),
'create_time',
'update_time',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
and here is my actionAdmin method on TeamController.php
public function actionAdmin()
{
$dataProvider=new CActiveDataProvider('Team', array(
'criteria'=>array(
'order'=>'create_time DESC',
),
'pagination'=>array(
'pageSize'=>20,
),
));
$this->render('admin',array(
'dataProvider'=>$dataProvider,
));
}
The relation is, obviously, a team belongs to a league.
You should just put the $data->league->league_name in quotes and then it will recognize the $data variable. It should look like this:
array(
'name'=>'league_id',
'type'=>'raw',
'value'=>'$data->league->league_name',
),
you need to do it like this :
your column should be
array(
'name'=>'league_id',
'value'=>array($this,'league_name'),
),
and you controller shod have a function like this:
public function league_name($data,$row)
{
return $data->league->league_name;
}
In the Type model's search() function, I want to display three attributes in CGridview:
type.name, type.description and count(dataset_type.dataset_id)
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->alias='t';
$criteria->select='t.name, t.description, count(dataset_type.dataset_id) as number';
$criteria->join='LEFT JOIN dataset_type ON dataset_type.type_id=t.id';
$criteria->group='t.id';
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
This is the view:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'type-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'itemsCssClass' => 'table table-bordered',
'columns' => array(
'name',
'description',
'number',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
but in this way the columns in CGridView can only display model attributes, it shows type.number can't find in model Type, how can I display the count()?
You will also have to add the attribute to your model to be able to use it as a column in your CGridView:
class Type extends CActiveRecord {
public $number;
I have 2 tables/models:
Tmp1
Header
QuestionText
Tmp2
Header
Part
OutOf
I'm trying to display the columns: Header, QuestionText, Part, OutOf
in a single CGRIDVIEW.
In Tmp1 model:
public function relations()
{
return array(
'tmp2s' => array(self::HAS_MANY, 'Tmp2', 'Header'),
);
}
In Tmp2 Model:
public function relations()
{
return array(
'header' => array(self::BELONGS_TO, 'Tmp1', 'Header'),
);
}
Controller:
public function actionReviewAll()
{
$tmp1 = new Tmp1('search');
$tmp1->unsetAttributes();
if(isset($_GET['Tmp1'])){
$model->attributes=$_GET['Tmp1'];
}
$this->render('ReviewAll',array(
'tmp1'=>$tmp1,
));
}
View:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'tmp-grid',
'dataProvider'=>$tmp1->search(),
'filter'=>$tmp1,
'columns'=>array(
'Header',
'QuestionText',
array(
'name' => 'tmp2.OutOf',
'value' => '$data->tmp2[0].OutOf',
'type' => 'raw'
),
),
)); ?>
Error:
Property "Tmp1.tmp2" is not defined.
Any help is greatly appreciated
You have a sintax error:
Tmp1.tmp2 = Tmp1.tmp2s
The relation alias is with 's'. "tmp2s".
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'tmp-grid',
'dataProvider'=>$tmp1->search(),
'filter'=>$tmp1,
'columns'=>array(
'Header',
'QuestionText',
'tmp2s.Part',
'tmp2s.OutOf',
),
)); ?>
But you are trying to show a 'self::HAS_MANY' relationship, you can't show that on a normal CGridView widget...
Your code probably works (I haven't tried it), but not in cases where your tmp1 model has no tmp2's. You must make sure there is a tmp2 before accessing it:
'value' => 'isset($data->tmp2) ? $data->tmp2[0].OutOf : Null',
You can also pass a function($data,$row) to value to make it look less messy. (Yes, that's important to me.)
I have a strange problem with my yii application. I have two tables in my database Players - id, name, team_id and Teams - id, name. I can create new players but when I want to see player's profile there is an error -"Trying to get property of non-object" at this line:
'value'=>$model->team->NAME,
The most strange problem is that when I test url for players with Ids 1 and 2 everything is okay and I see the correct information, but for other ids I have this problem. Here is part of my code:
view.php
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'ID',
'NAME',
'TEAM_ID',
array(
'label'=>'Отбор',
'type'=>'text',
'value'=>$model->team->NAME,
),
),
)); ?>
Players.php
public function relations()
return array(
'team' => array(self::BELONGS_TO, 'TEAMS', 'ID'),
);
}
Teams.php
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'player' => array(self::HAS_MANY, 'PLAYERS', 'ID'),
);
}
PlayersController.php
public function actionView($id)
{
$teams = new CActiveDataProvider('Teams');
$players = new CActiveDataProvider('Players');
$this->render('view', array(
'model'=>$this->loadModel($id),
));
}
Seems like you need to fix bug with relations in Players model:
public function relations()
return array(
'team' => array(self::BELONGS_TO, 'TEAMS', 'TEAM_ID'), // TEAM_ID instead of ID
);
}