[英]Yii Relations Active Record with primary key
我有两个表:
学生
ID,名称,电子邮件等。
主题(具有外键student_ibfk_1)
id,student_id,subject_id,分数
我定义了两个模型:
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 :我也尝试过
班级学生扩展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,...)”。
您必须命名外键字段,而不是外键名称。 所以应该
return array(
'subjectMember' => array(self::BELONGS_TO, 'Student', 'id'),
}
但您可以省略id
,因为它默认为id
:
return array(
'subjectMember' => array(self::BELONGS_TO, 'Student'),
}
在mysql中创建所有外部关系,然后使用gii创建模型更加容易。 它将为您创建所有关系。
尝试
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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.