2

我在代理模型中有一个 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',
    ),
)); ?>

任何帮助将不胜感激。

谢谢抗体

4

1 回答 1

2

首先,您应该像这样与学生建立关系到relations数组中的关系表relation model..

'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));
于 2012-05-15T06:01:27.913 回答