Yii 1.1.14
I have an employee table and a comment table
In the employee view i want to show all the comments of the employee in a grid (after the employee fields)
I tried to follow the example here
Here is where I am :
New code to defining a new search function in the model :
public $commentdate_param;
public $commentobservation_param;
...
public function rules()
{
return array(
...
array('public $commentdate_param, commentobservation_param, ...', 'safe', 'on'=>'search, searchIncludingComments'),
);
public function relations()
{
return array(
...
'employeecomments' => array(self::HAS_MANY, 'Employeecomments', 'employee_id'),
);
}
public function searchIncludingComments($parentID)
{
$criteria=new CDbCriteria;
$criteria->with=array('employeecomments');
$criteria->together = true;
$criteria->compare('t.employee_id',$parentID,false);
$criteria->compare('employeecomments.date', $this->commentdate_param,true);
$criteria->compare('employeecomments.observation', $this->commentobservation_param,true);
$sort = new CSort;
$sort->attributes = array(
'commentdate_param' => array(
'asc' => 'date_desc',
'desc' => 'date_desc DESC',
), '*', /* Treat all other columns normally */
);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>$sort,
));
}
Adding code in the controller actionView :
...
$child_model = new Employee("searchIncludingComments");
$child_model->unsetAttributes();
$this->render('view',array(
'model'=>$this->loadModel($id),
'child_model'=>$child_model,
'parentID' => $id
));
Adding code in the view :
...
<h3>Comments</h3>
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'child-grid',
'dataProvider'=>$child_model->searchIncludingComments($parentID),
'filter'=>$child_model,
'columns'=>array(
'date',
'observation',
array(
'class'=>'CButtonColumn',
),
),
));
...
But I must have missed something because when it comes to grid rendering stops - but no error in log
Would be nice if somebody could help me !
I found the solution for me - all my changes and controler are worthless because now I do all stuff in the view :
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'comments-grid',
'dataProvider'=>new CActiveDataProvider('Employeecomment',array(
'criteria'=>array(
'condition'=>'employee_id=:eid',
'params'=>array(':eid'=>$model->id),
),
'sort'=>array(
'defaultOrder'=>'date DESC',
),
)),
Sure there is something I did not understand why it did not work ...
Related
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;
}
I want to show 2 tables with a right join, but the code I wrote does not work as expected. Can anyone tell me what I am doing wrong ?
view : admin.php
$this->widget('bootstrap.widgets.TbGridView', array(
'id' => 'punish-grid',
'dataProvider' => $model->searchJoin(),
'type' => 'striped bordered condensed',
'filter' => $model,
'columns' => array(
array(
'header' => 'No',
'type'=>'raw',
'htmlOptions'=>array('style'=>'width: 25px'),
'value'=>'$this->grid->dataProvider->pagination->currentPage
*$this->grid->dataProvider->pagination->pageSize + $row+1',
),
// i want to display p.kode,p.status from table status
'berlaku_punish',
'nilai',
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
),
),
));
and my model : BasePunish.php
public function relations() {
return array(
'idStatus' => array(self::BELONGS_TO, 'Status', 'id_status'),
);
}
public function searchJoin() {
$criteria = new CDbCriteria;
$criteria->select = 'p.kode,p.status,t.nilai,t.berlaku_punish';
$criteria->join= 'RIGHT JOIN status p ON (t.id_status=p.id)';
$criteria->condition = 't.id_status IS NULL';
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'sort'=>array(
'defaultOrder'=>'kode ASC',
),
)
);
}
I might really did not understand what you are asking but still if nothing is working for you then you can try this
array(
'header'=>'Products',
'value'=>array($model,'gridCreateUser'),
),
In this, the value will try to find the function gridCreateUser in the class of which $model is the object. In your case i guess $model is the object of the BasePunish.
So in your BasePunish.php create a function gridCreateUser() and then you can return the value which you want to display in your widget.
eg:-
In your BasePunish.php
public function gridCreateUser($data)
{
// you can access the object $data here
// do what ever you want to do
$value='return whatever you want to return';
return $value;
// this $value will be displayed there
}
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 am trying to set up a MANY_MANY Relationship in Yii using Active Record.
I have three tables
profile
profile_id
profile_description
category
category_id
category_name
profile_category
profile_id
category_id
My models are Profile, Category, and ProfileCategory.
I am trying to run a query using the category_id that will pull up all of the profiles that are in that category.
This is the information in the category model.
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(
'profiles'=>array(
self::MANY_MANY,
'Profile',
'profile_category(category_id, profile_id)',
),
'profile_category'=>array(
self::HAS_MANY,
'ProfileCategory',
'category_id',
),
);
}
Profile Model
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(
'categories'=>array(
self::MANY_MANY,
'Category',
'profile_category(profile_id, category_id)'
),
'profileCategory'=>array(
self::HAS_MANY,
'ProfileCategory',
'profile_id'
),
);
}
ProfileCategory Model
public function relations()
{
return array(
'category'=>array(
self::BELONGS_TO,
'Category',
'category_id',
),
'profile'=>array(
self::BELONGS_TO,
'Profile',
'profile_id',
),
);
}
Controller
public function actionResults()
{
$category=$_POST['terms'];
$dataProvider=new CActiveDataProvider(
'Profile',
array(
'criteria'=>array(
'with'=>array('profile_category'),
'condition'=>'display=10 AND profile_category.category_id=1',
'order'=>'t.id DESC',
'together'=>true,
),
)
);
$this->render('results',array(
'dataProvider'=>$dataProvider,
));
}
View
<div id=resultsleft>
<?php
foreach($dataProvider as $value)
{
echo $value->profile_id;
}
?>
</div>
Any thoughts? Thank You! Nothing shows in the view.
You have to set up a property called profileCategory (not profile_category) in the profile model:
'profileCategory'=>array(
self::HAS_MANY,
'ProfileCategory',
'profile_id'
),
You can use it with an and condition as follows:
'criteria'=>array(
'with'=>array('profileCategory'),
'condition'=>'display=10 AND profileCategory.category_id=1',
'order'=>'t.id DESC',
'together'=>true,
),
It would be better for logical id first to model relation...
An example is available here.
Category model
'Profile', 'profile_category(profile_id, ...)'
public function relations()
{
return array(
'profiles'=>array(
self::MANY_MANY,
'Profile',
'profile_category(profile_id, category_id)'
),
'profile_category'=>array(
self::HAS_MANY,
'ProfileCategory',
'category_id',
),
);
}
Profile model
'Category', 'profile_category(category_id, ...)'
public function relations()
{
return array(
'categories'=>array(
self::MANY_MANY,
'Category',
'profile_category(category_id, profile_id)'
),
'profile_category'=>array(
self::HAS_MANY,
'ProfileCategory',
'profile_id'
),
);
}
Its actualy if model of database has
many self::BELONGS_TO and database has only PK (PrimaryKeys)
such code will be correctly executed
print_r(Profile::model()->findByPk(5)->categories);
print_r(Category::model()->findByPk(8)->profiles);
I have searched and searched for an answer to my problem but neither of them worked. My problem is regarding the filtering/searching in the CGridView for Yii.
In my Applicant model I have the following:
private $_city = null;
private $province_id = null;
public function getCity()
{
if($this->_city === null && $this->address_id !== null) {
$this->_city = $this->address->city;
}
return $this->_city;
}
public function setCity($value)
{
$this->_city = $value;
}
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->with = array('address'=>array('alias'=>'address'));
$criteria->compare('id',$this->id);
$criteria->compare('phn',$this->phn);
$criteria->compare('gender',$this->gender);
$criteria->compare('dob',$this->dob,true);
$criteria->compare('first_name',$this->first_name,true);
$criteria->compare('last_name',$this->last_name,true);
$criteria->compare('band_id',$this->band_id);
$criteria->compare('note',$this->note,true);
$criteria->compare('address.city',$this->city);
$criteria->compare('address.province_id',$this->province_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'attributes'=>array(
'id',
'phn',
'first_name',
'last_name',
'band_id',
'city'=>array(
'asc'=>'address.city ASC',
'desc'=>'address.city DESC',
),
'province_id'=>array(
'asc'=>'address.province_id ASC',
'desc'=>'address.province_id DESC',
),
)
)
));
}
In my View I have
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'applicants-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'first_name',
'last_name',
'phn',
array(
'name'=>'band_id',
'value'=>'$data->band->name',
'filter'=>CHtml::listData(Band::model()->findAll(), 'id', 'name')
),
array(
'name'=>'city',
'value'=>'$data->address->city', // causes 'Trying to get property of non-object' - no error if $data->city
'filter'=>CHtml::listData(Address::model()->findAll(), 'city', 'city'),
),
array(
'class'=>'CButtonColumn',
'template'=>'{view}',
'buttons'=>array(
'view'=>array(
'url'=>'Yii::app()->createUrl("/applicant/view", array("id"=>$data->id))',
)
)
),
)
));
I have the cities and drop down list showing but when I choose are city from the dropdown it does not filter the widget with the proper results.
I have tried doing the solutions in the following pages
http://www.yiiframework.com/forum/index.php?/topic/11546-cgridview-with-mulitple-model/
http://www.yiiframework.com/forum/index.php?/topic/19913-cgridview-with-multiple-models/
Are you searching on the primary key of the city table in your compare statement?
E.g., if the PK of city is "city_id" you would need to change the lines below in your model and view, respectively:
$criteria->compare('address.city_id',$this->city);
'filter'=>CHtml::listData(Address::model()->findAll(), 'city_id', 'city')
(In this example you would leave "city" as the name so it displays as text in the grid, but specify the PK, city_id, in the listData in the value attribute.)