Reputation: 139
I have two tables and i need to filter and put those two table data to gridview.i use joined two table like this
$student=new Student;
$marks=new AssimentMarks;
$criteria_st=new CDbCriteria;
$criteria=new CDbCriteria;
$criteria->select = 't.st_id,t.st_name,stu.ass_id,stu.marks_cr1,stu.marks_cr2,stu.marks_cr3,stu.marks_cr4,stu.marks_cr5';
$criteria->join = 'INNER JOIN assiment_marks stu ON stu.st_id=t.st_id';
$criteria->condition = 'stu.ass_id=:ass_id';
$criteria->params = array(':ass_id'=>Yii::app()->session['modelcrite']['ass_id']);
$criteria->addInCondition('t.st_id', $studentid);
return new CActiveDataProvider($student, array('criteria'=>$criteria,));
but in gridviwe only show the student database values.it is show as .how can i pass two models to CActiveDataProvider ?
this is how data shows https://i.sstatic.net/Kogjz.jpg
Upvotes: 0
Views: 1764
Reputation: 10177
Your assiment table should reference your student table with a foreign key, like this (assuming SQLite):
CREATE TABLE student (
id INTEGER PRIMARY KEY,
name TEXT
);
CREATE TABLE assiment_record(
id INTEGER PRIMARY KEY,
student_id INTEGER,
cr1 INTEGER,
cr2 INTEGER,
cr3 INTEGER,
cr4 INTEGER,
cr5 INTEGER,
FOREIGN KEY( student_id ) REFERENCES student(id) -- FK goes in the CHILD table.
);
student -> models/Student.php
assiment_record -> models/AssimentRecord.php
Gii is smart and guesses your relations:
// AssimentRecord.php looks good! No changes made!
public function relations()
return array(
'student' => array(self::BELONGS_TO, 'Student', 'student_id'),
);
The assiment record BELONGS TO the student.
But Gii's is not perfect, and does not know what your intentions are. The current student relation (HAS_MANY) returns an array, but I don't want to deal with the array (because I'm lazy!), so I change it into a HAS_ONE-relation instead:
// Student.php
public function relations()
{
return array(
// Change this: 'assimentRecords' => array(self::HAS_MANY, 'AssimentRecord', 'student_id'),
/* into this: */ 'assimentRecord' => array(self::HAS_ONE, 'AssimentRecord', 'student_id'),
);
}
The student HAS ONE assiment record.
// views/site/index.php (Or wherever.)
$dataProvider = new CActiveDataProvider( 'Student' );
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns' => array(
'id',
'name',
'assimentRecord.cr1',
'assimentRecord.cr2',
'assimentRecord.cr3',
'assimentRecord.cr4',
'assimentRecord.cr5',
)
));
Upvotes: 3