我在代理模型中有一个 GetStudents 函数。我在那里获取与代理相关的学生。
但我想显示另一个表中学生的更多字段 例如
**Agent table** (agent has students)
agent_id
**student table**
STUDENTID pkey
agent_id
**relation table** (this table creates relation between student and household)
StudentID HOUSEHOLDID
**HOUSEHOLD TABLE**
HouseHOLDID Householdname
我想在获取所有学生详细信息时获取家庭姓名。
对我来说真的很奇怪,因为我是 YII 的新手
代理模型中的我的函数。
public static function getStudents($id) {
$relationships = Relationship::model()->findAll('type = :type AND source = :agent_id', array(':type' => 2, ':agent_id' => $id));
//$relationships=sort($relationships);
// Generate relationship array
//print_r($relationships);
foreach ($relationships as $relationship) {
$in[] = $relationship->destination;
}
// Generate db condition
$criteria = new CDbCriteria;
if (! isset($in) || ! is_array($in) || sizeOf($in) == 0) {
$in[] = -999;
}
$criteria->addInCondition('StudentID', $in, 'OR');
return new CActiveDataProvider('Student', array(
'criteria' => $criteria,
'sort'=>array('defaultOrder'=>array(
'StudentID'=>CSort::SORT_DESC,
)),
));
}
我的代码通过将 ID 传递到其中来获取数据
<?php $this->widget('zii.widgets.CDetailView', array(
'data' => $model,
'attributes' => array(
array(
'label' => 'Agent Details',
'type' => 'raw',
'value' => '',
'cssClass' => 'heading',
),
'agent_id',
'user.email',
'first_name',
'last_name',
'company',
'phone',
),
)); ?>
任何帮助将不胜感激。
谢谢 抗体
最佳答案
首先,您应该在关系模型
中的关系
数组中与学生建立关系,如下所示..
'student'=>array(self::BELONGS_TO, 'Student', 'StudentID'), //after belongs to student is model class name
'household'=>array(self::BELONGS_TO, 'HOUSEHOLD', 'HOUSEHOLDID'),//after belongs to HOUSEHOLD is model class name
然后您可以像这样获取具有事件记录的所有记录...
$relationships = Relationship::model()->with('student','household')->findAll('type = :type AND source = :agent_id', array(':type' => 2, ':agent_id' => $id));
关于yii - YII 中的关系问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10594748/