Prashant
Prashant

Reputation: 2065

Yii Relations Active Record with primary key

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,...)".

Upvotes: 0

Views: 696

Answers (2)

darkheir
darkheir

Reputation: 8950

Try

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::HAS_MANY, 'Subject', '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', 'id'),
    }
}

Both of your relations were wrong.

Then in your criteria you are calling eventMember.subject_id

$criteria->condition='`t`.`name`=:name AND `eventMember`.`subject_id`=:subject_id';

But nowhere I see the table eventMember.

Upvotes: 0

chris---
chris---

Reputation: 1546

You have to name the foreign key field, not the foreign key name. So it should be

return array(
    'subjectMember' => array(self::BELONGS_TO, 'Student', 'id'),
}

but you can leave the id out because it defaults to id:

return array(
    'subjectMember' => array(self::BELONGS_TO, 'Student'),
}

It's easier to create all the foreign relations in mysql and then create the models with gii. It will create all the relation for you.

Upvotes: 1

Related Questions