Yii Relations Active记录与主键

时间:2014-08-06 08:52:40

标签: php activerecord yii foreign-keys relationship

我有两张桌子:

学生

id,姓名,电子邮件等。

主题(它有一个外键student_ibfk_1)

id,student_id,subject_id,mark

我定义了两个模型:

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)'),
    }
}

我想使用关系来为使用AR模型的主题获取学生的标记。

我称之为:

$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);

我收到错误。

preg_match()期望参数2为字符串,给定数组

你告诉我,我哪里错了?我正在关注Yii文档http://www.yiiframework.com/doc/api/1.1/CActiveRecord#relations-detail

EDIT1 :我也试过

class Student扩展了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'),
    }
}

这给了我错误:

关系"主题"在活跃的记录班"主题"使用无效的外键指定。外键的格式必须是" joinTable(fk1,fk2,...)"。

2 个答案:

答案 0 :(得分:1)

您必须命名外键字段,而不是外键名称。所以它应该是

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

但您可以退出id,因为它默认为id

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

在mysql中创建所有外部关系然后使用gii创建模型更容易。它会为你创造所有的关系。

答案 1 :(得分:0)

尝试

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'),
    }
}

你们两个人的关系都错了。

然后在您的条件中,您正在调用eventMember.subject_id

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

但我无处可见表eventMember