I have two tables:
Student
id, name, email, etc..
Subject (It has a foreign key student_ibfk_1)
id, student_id, subject_id, marks
I have both my models defined:
class Student extends CActiveRecord{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
....
....
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(
'subjectMember' => array(self::MANY_MANY, 'Subject', array('id'=>'student_id')),
}
}
class Subject extends CActiveRecord{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
....
....
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(
'subjectMember' => array(self::BELONGS_TO, 'Student', 'student_ibfk_1(id)'),
}
}
I want to use relations to fetch marks for a student for a subject using AR model.
I am calling this as:
$criteria = new CDbCriteria();
$criteria->condition='`t`.`name`=:name AND `eventMember`.`subject_id`=:subject_id';
$criteria->params=array(':name'=>$name, ':subject_id'=>$subject_id);
$avc = Student::model()->with('subjectMember')->fetchAll($criteria);
And I receive an error.
preg_match() expects parameter 2 to be string, array given
Can you tell me, where am I going wrong? I am following the Yii documentation http://www.yiiframework.com/doc/api/1.1/CActiveRecord#relations-detail
EDIT1: I also tried
class Student extends CActiveRecord{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
....
....
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(
'subjectMember' => array(self::MANY_MANY, 'Subject', 'subject(student_ibfk_1)'),
}
}
class Subject extends CActiveRecord{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
....
....
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(
'subjectMember' => array(self::BELONGS_TO, 'Student', 'id'),
}
}
This gives me the error:
The relation "subject" in active record class "Subject" is specified with an invalid foreign key. The format of the foreign key must be "joinTable(fk1,fk2,...)".