yii - YII 中的关系问题

标签 yii

我在代理模型中有一个 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/

相关文章:

Yii:没有数据库服务器的单元测试模型

php - 单一缓存前端和后端

php - 过滤 gridview 后的 Yii2 导出

php - Yii 不知道我的数据库发生了变化

php - 登录在 localhost 中工作,但登录在远程 yii 中不工作

php - 搜索关系数据

mysql - 使用 yii 框架测试和生产服务器部署 - 同步数据库更改

php - : how to ajax update the cgridview yii

frameworks - Yii ,从 $_FILES 保存图像,但不使用模型

php openssl_encrypt 在使用 openssl_decrypt 时生成空值