i have 2 tables (STUDENT AND EMPLOYEE)
student(reg_no , s_name, dept, f_name) reg_no is primary key
employee(e_no,design,salary,reg_no) reg_no is foreign key reference student(reg_no).
i want to display e_no,design from employee
and s_name,dept from student table.
so my complete cgridview would be
e_no, design,s_name,dept
my code for employee/admin.php
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'employee-grid',
'dataProvider'=>$model->searchEmployees(),
//'filter'=>$model,
'columns'=>array(
'e_no',
'e_name',
'design',
'salary',
'reg_no',
array('name'=>'student.s_name', 'value'=>'$data->student->s_name'), // student name
'salary', // employee.salary
array(
'class'=>'CButtonColumn',
),
),
));
?>
my code for model/employee.php for searchEmployees()
public function searchEmployees()
{
$criteria=new CDbCriteria;
$criteria->alias = 'i';
$criteria->compare('e_no',$this->e_no);
$criteria->compare('e_name',$this->e_name,true);
$criteria->compare('design',$this->design,true);
$criteria->compare('salary',$this->salary);
$criteria->compare('reg_no',$this->reg_no);
$criteria->join= 'JOIN student d ON (i.reg_no=d.reg_no)';
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'reg_no ASC',
),
));
}
BUT I FACE ERROR
Property "Employee.studentname" is not defined.
HOW TO SOLVE THE ERROR PLZ HELP.
THANKS
You can configure the cgridview to display information for related records using CActiveRecord->getRelated in your customization of columns... it'd be as follows:
student(reg_no , s_name, dept, f_name) would be model Student.
employee(e_no,design,salary,reg_no) would be model Employee.
Employee.e_no, Employee.design, Student.s_name and Student.dept are your desired values so your columns array should be:
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'student-grid',
'dataProvider'=>Student::model()->search(),
//'filter'=>$model,
'columns'=>array(
array(
'header'=>Employee::model()->getAttributeLabel('e_no'), //column header
'value'=>'$data->getRelated('employee')->e_no', //column name, php expression
'type'=>'raw',
),
array(
'header'=>Employee::model()->getAttributeLabel(\'design\'), //column header
'value'=>'$data->getRelated(\'employee\')->design', //column name, php expression
'type'=>'raw',
),
's_name',
'dept',
),
));
?>
Notice how related record data is done in the 'value' specification of a custom column, and that the argument to getRelated() is the name of the relationship defined to the Employee model defined in the Student->relations() method. Also check CGridView documentation on columns for more info on how this works.
Update: You can easily switch the approach of this CGridView instantiation to use an Employee instead of a Student and still display the same info by changing the columns array.
Update 2: You can easily handle non existing relationships in the columns array:
'columns'=>array(
array(
'header'=>Employee::model()->getAttributeLabel('e_no'), //column header
'value'=>'($data->getRelated(\'employee\')=== null)?'No related employee registry':$data->getRelated('employee')->e_no', //column name, php expression
'type'=>'raw',
),
array(
'header'=>Employee::model()->getAttributeLabel(\'design\'), //column header
'value'=>'($data->getRelated(\'employee\')=== null)?'No related employee registry':$data->getRelated(\'employee\')->design', //column name, php expression
'type'=>'raw',
),
's_name',
'dept',
),
With this tweaks in the 'value' fields of our members in 'columns' we display a small message whenever the model tries to access a related model. getRelated() returns the related record(s) or null if none is found. Maybe you have Students an equivalent Employee model, which might be the cause of the error. I hope the above helps you resolve it.
Related
I am doing server side processing of datatables and I want to show extra col merged within a col, for eg. my table looks like:
id
name
age
price
Its all working fine, but I want to display another col in the price col which I am not able to get
For example: I want it to show like
id
name
age
price cur_code
I am trying the following
array(
'db' => 'serv_price',
'dt' => 8,
'formatter' => function( $d, $row ) {
return $d . " " . $row["serv_cur_code"];
}
),
but its not displaying it, how to get cur_code extra field ?
Thanks in Advance.
I have a cpt "members".
In a post I can select multiple members (object) via ACF in the specific post.
Now I want say: Please give me all posts where a member (e.g. nr. 76) ist marked in the post.
I try the whole day, and have no idea.
It's possible if i change the acf field to one number to get the correct posts, but Its important that I can select multiple members.
Thanks for helping!
Finally got it :-)
$args = array(
'post_type'=>'post',
'numberposts'=> -1,
'meta_query' => array(
array(
'key' => 'members',
'value' => '76', //example
'compare'=>'LIKE'
)
)
);
i'm not good with sql, i'm making a CListView for all records with a selected id in column to be displayed on the page.. just for practice, i wanted to add a dropdown that will select another kind of sort (id DESC is already there).
i have a table with columns: id, Name, Project_id, User_id, Assigned, etc...
for now this is my code:
public function actionView($id)
{
$model = $this->loadModel($id);
$criteria = new CDbCriteria(array(
'order'=>'id desc',
'condition'=>'Project_id='.$id
));
$dataProvider = new CActiveDataProvider('Task', array(
'criteria'=>$criteria
));
$this->layout = 'column2';
$this->render('view', array('model'=>$model, 'dataProvider'=>$dataProvider));
}
i'm selecting all records that has the requested project_id, so what i'm trying to do, which i have no clue how to do, is to add another criteria selection on the column "Assigned" the problem is, some of this records has more than 1 single assigned and its being saved as "1,2,5,6". so if i add another selection with assigned 1, it will just show me thoses records that have in assigned "1" and not thoses that have "1,3,5,6".. i was thinking on make a search on string but i dont know how it works on sql (Because i dont know SQL that much)
Try this -
$criteria = new CDbCriteria(array(
'order'=>'id desc',
'condition'=>'Project_id='.$id.' AND find_in_set($assigned, Project_id)'
));
But here $assigned can have only single value i.e. $assigned = 1 or $assigned = 2.
If $assigned = "1,2" where 1,2 itself will be considered as single value and so result will be returned only if the pattern is 1,2.. in the table and not 2,1,...
I hope it gives some idea.
found my solution.. seems like im using SQLite and SQLite does not support find_in_set(), instead. i would have to use (',' || Assigned || ',') LIKE '%,1,%'. like this..
$criteria = new CDbCriteria(array(
'order'=>'id desc',
'condition'=>'Project_id='.$id.' AND ("," || Assigned || ",") LIKE "%,5,%"'
));
I want to display only the data in the dataprovider for today's date only. $data->timedate is the appointment date. If it is equal to the current date, display it. The following code does not work.
//today appointments dataprovider
$taProvider=new CActiveDataProvider('Appointments',array(
'sort'=>array(
'defaultOrder'=>'datetime ASC',
),
'criteria'=>array(
'condition'=>'cId=:cId',
'params'=>array(':cId'=>Yii::app()->user->id),
'condition'=>$data->timedate = date('Y-m-d'),
),
));
EDIT Modifiyed for using CDbcriteria object
You are using the PHP variables within CDbCriteria->condition. CDbCriteria Condition is nothing but the where clause in your sql query http://www.yiiframework.com/doc/api/1.1/CDbCriteria#condition-detail
Also your second assignment would overwrite the first if you need to add condition you need to use addCondition() method
Change
'criteria'=>array(
'condition'=>'cId=:cId',
'params'=>array(':cId'=>Yii::app()->user->id),
'condition'=>$data->timedate = date('Y-m-d'),
),
to
'criteria'=>array(
'condition'=>"DATE(t.timedate) = DATE(NOW()) AND cId=:cId ",
'params'=>array(':cId'=>Yii::app()->user->id),
),
I have two table: 'Streams' and 'Punches'. One stream have many punches.
In my view, I use 'CListView' to show all punch's image of a stream.
But I want show the stream's image in first page, and all punch's image will show on next pages.
How can I do that? thanks...
Ah, I resolved this problem... Instend of using CActiveDataProvider, I use CSqlDataProvider to "union" two table.
$count=Yii::app()->db->createCommand('SELECT COUNT(*) FROM tbl_user')->queryScalar();
$sql='SELECT * FROM streams union SELECT * FROM punches';
$dataProvider=new CSqlDataProvider($sql, array(
'totalItemCount'=>$count,
'sort'=>array(
'attributes'=>array(
'id', 'username', 'email',
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
// $dataProvider->getData() will return a list of arrays.