php - CDBCriteria 联接不返回所选的第二个和第三个表的数据

标签 php mysql yii

我在从数据库中检索数据时遇到问题,我已将表 user 三次加入到表 family_tree 中。 CdbCommand 会返回从第一个表(family_tree)中选择的值,但不会返回从第二个和第三个表(user)中选择的数据。

表的架构是:

用户
用户 ID |用户全名

家谱
tree_creator_id | tree_user1_id | tree_user2_id

模型/familytree.php:

public function search()
{
    // @todo Please modify the following code to remove attributes that should not be searched.

    $criteria=new CDbCriteria;
    //$criteria->alias='FamilyTree';
    $criteria->select='t.*,A.*,B.*,C.*';        
    $criteria->join = 'join user A on A.user_id=tree_user1_id 
    join user B on B.user_id=tree_user2_id 
    join user C on C.user_id=tree_creator_id';

    $criteria->compare('tree_id',$this->tree_id,true);          
    $criteria->compare('tree_creator_id',$this->tree_creator_id,true);
    $criteria->compare('tree_user1_id',$this->tree_user1_id,true);
    $criteria->compare('tree_user2_id',$this->tree_user2_id,true);
    $criteria->compare('tree_type',$this->tree_type,true);
    $criteria->compare('tree_type_name',$this->tree_type_name,true);
    $criteria->compare('tree_grey_flag',$this->tree_grey_flag);
    //$criteria->join('user', 'tree_creator_id=user.user_id');
    //$criteria->compare('tree_user_id',$this->A->user_fullname, true);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

查看/familytree/admin.php

<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
'model'=>$model,
)); ?>
</div><!-- search-form -->

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'family-tree-grid',
'dataProvider'=>$model->search(),

'columns'=>array(
    'tree_id',
    'tree_creator_id',
    'C.user_fullname',
    'tree_user1_id',
    'A.user_fullname',
    'tree_user2_id',
    'B.user_fullname',
    'tree_type',
    'tree_type_name',

    /*'tree_grey_flag',*/

    array(
        'class'=>'CButtonColumn',
    ),
),
)); ?>

最佳答案

请你试试下面的代码:

model/familytree.php

/**
 * @return array relational rules.
 */
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(
        'treeCreaterRel' => array(self::BELONGS_TO, 'user', 'tree_creator_id'),
        'treeUser1Rel' => array(self::BELONGS_TO, 'user', 'tree_user1_id'),
        'treeUser2Rel' => array(self::BELONGS_TO, 'user', 'tree_user2_id'),

    );
}

public function search()
{
    // @todo Please modify the following code to remove attributes that should not be searched.

    $criteria=new CDbCriteria;


    $criteria->with = array('treeCreaterRel', 'treeUser1Rel', 'treeUser2Rel');
    $criteria->together = true;

    $criteria->compare('tree_id',$this->tree_id,true);          
    $criteria->compare('tree_creator_id',$this->tree_creator_id,true);
    $criteria->compare('tree_user1_id',$this->tree_user1_id,true);
    $criteria->compare('tree_user2_id',$this->tree_user2_id,true);
    $criteria->compare('tree_type',$this->tree_type,true);
    $criteria->compare('tree_type_name',$this->tree_type_name,true);
    $criteria->compare('tree_grey_flag',$this->tree_grey_flag);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
        'pagination'=>array(
            'pageSize'=>'10',  //Yii::app()->params['defaultPageSize'],
            //'currentPage'=>$currentPage
        ),
        'sort'=>array(
            'defaultOrder'=>array(
                'tree_id'=>CSort::SORT_DESC
            ),
        ),
    ));
}

查看/familytree/admin.php

<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>

<div class="search-form" style="display:none">
    <?php $this->renderPartial('_search',array(
    'model'=>$model,
    )); ?>
</div><!-- search-form -->

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'family-tree-grid',
'dataProvider'=>$model->search(),

'columns'=>array(
    array(
        'header' => 'Tree Id',
        'name' => 'tree_id',
        'value' => '$data->tree_id'
    ),
    array(
        'header' => 'Tree Creater Id',
        'name' => 'tree_id',
        'value' => '$data->tree_id'
    ),
    array(
        'header' => 'Tree Creater Name',
        'name' => 'tree_creator_id',
        'value' => '$data->treeCreaterRel->user_fullname'
    ),
    array(
        'header' => 'Tree User1 Name',
        'name' => 'tree_user1_id',
        'value' => '$data->treeUser1Rel->user_fullname'
    ),
     array(
        'header' => 'Tree User1 id',
        'name' => 'tree_user1_id',
        'value' => '$data->tree_user1_id'
    ),
    array(
        'header' => 'Tree User2 Name',
        'name' => 'tree_user2_id',
        'value' => '$data->treeUser2Rel->user_fullname'
    ),
    array(
        'header' => 'Tree User2 id',
        'name' => 'tree_user2_id',
        'value' => '$data->tree_user2_id'
    ),
    'tree_type',
    'tree_type_name',
    array(
        'class'=>'CButtonColumn',
    ),
),
)); ?>

关于php - CDBCriteria 联接不返回所选的第二个和第三个表的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32706647/

相关文章:

php - Wordpress 中的 Codeigniter 如何配置 Nginx

php - Imagick 和 phmagick : Postscript delegate failed/No such file or directory 问题

php - 如何在 <li> yii bootstrap navbar 中的 <a> 标签中添加一个类

java - 将php页面引入java代码中

javascript - 提交表格并停留在同一页面上?

mysql - "Insert into where"语句错误

mysql - 检查数据库模式 mysql

mysql - sql Match() 针对无法识别的情况

php - T_PAAMAYIM_NEKUDOTAYIM 覆盖 CButtonColumn 后出错

php - 对于初学者来说,Laravel 和 Yii 框架哪个更容易理解?