YII中的关系问题

YII中的关系问题,yii,Yii,我在代理模型中有一个GetStudents函数。在那里我找到了与特工有关的学生 但我想从另一张桌子上展示更多的学生领域 乙二醇 当我获取所有学生详细信息时,我想获取householdname 我真的很期待bazzare,因为我是YII的新手 我在agent模型中的作用 public static function getStudents($id) { $relationships = Relationship::model()->findAll('type = :type

我在代理模型中有一个GetStudents函数。在那里我找到了与特工有关的学生

但我想从另一张桌子上展示更多的学生领域 乙二醇

当我获取所有学生详细信息时,我想获取householdname

我真的很期待bazzare,因为我是YII的新手

我在agent模型中的作用

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',
    ),
)); ?>

任何帮助都将不胜感激

谢谢
Ab

首先,您应该像这样在
关系模型中的
关系中的
数组中与student-to-relation表建立关系

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

嗨,谢谢你的快速回复。。但是关系表在哪里使用呢?我有一个带有StudentID HOUSEHOLDID字段的关系表,它建立了student和HOUSEHOLDID之间的关系。再次感谢,关系是你的榜样,与(‘学生’、‘家庭’)建立关系是你的榜样。。此处,您在..关系“学生”中使用的关系未在活动记录类“关系”中定义

'$relationships=Relationship::model()->with('student','household')->findAll('type=:type AND source=:agent\u id',array(':type'=>2',:agent\u id'=>$id));“'public function relations(){return array('user'=>array(self::HAS_ONE,'Users','user_id'),'student'=>array(self::belies_TO,'student','StudentID'),'HouseholdID');}让我们看看家庭关系应该在学生模型中,不在代理模型中。此外,您是否需要一个学生家庭链接表取决于。如果学生可以有很多家庭,那就是一种有很多关系的关系。如果家庭也可以与许多学生建立关系,那么您需要一个多::多关系
 $relationships = Relationship::model()->with('student','household')->findAll('type = :type AND source = :agent_id', array(':type' => 2, ':agent_id' => $id));