如何在Yii模型搜索方法中添加?

时间:2013-07-12 18:31:15

标签: yii cgridview

我的数据库中有四个表(如用户,user_test,test,status)。用户和测试之间存在M:M关系,因此user_test是它们之间的Gerund。 状态表具有user_test和测试表的状态。状态表中的tablename字段显示状态所属的表,如下图所示。

我想在CGridView中显示表user_test,其中包含用户,测试和状态三个表中的相关数据。一切都很好,关系是正确的。

问题:当我想在我的CGrid中显示status.name时:

array(
    'header'=>'Status',
    'value'=>'$data->status->name',
), 

它给我的名字'已完成',但它是表'test'的状态,正确的应该'确认',因为user_test中的status_id是2。

任何帮助?

Database design Status table

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

    $criteria=new CDbCriteria;
    $criteria->with=array('user','test','status');

    $criteria->compare('id',$this->id);
    $criteria->compare('user_id',$this->user_id);
    $criteria->compare('test_id',$this->test_id);
    $criteria->compare('status.id',$this->status_id);
    $criteria->compare('bonus',$this->bonus);
    $criteria->compare('user.signum',$this->signum, FALSE);
    $criteria->compare('user.email',$this->email, FALSE);
    $criteria->compare('test.seats',$this->seats, FALSE);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
        'sort'=>array(
            'attributes'=>array(
                'signum'=>array(
                    'asc'=>'user.signum',
                    'desc'=>'user.signum DESC',
                ),
                'email'=>array(
                    'asc'=>'user.email',
                    'desc'=>'user.email DESC',
                ),
                'seats'=>array(
                    'asc'=>'test.seats',
                    'desc'=>'test.seats DESC'
                ),
                '*',
            ),
        ),
    ));
}

更新: 关系在这里:

//UserTest
....
return array(
        'status' => array(self::BELONGS_TO, 'Status', 'status_id'),
        'test' => array(self::BELONGS_TO, 'Test', 'test_id'),
        'user' => array(self::BELONGS_TO, 'User', 'user_id'),
        'testtimeslots'=>array(self::BELONGS_TO, 'TestTimeslots','timeslots_id'),
    );


 // Test
  return array(
        'testType' => array(self::BELONGS_TO, 'TestType', 'test_type_id'),
        'status' => array(self::BELONGS_TO, 'Status', 'status_id'),
        'testCriterias' => array(self::HAS_MANY, 'TestCriteria', 'test_id'),
        'testTimeslots' => array(self::HAS_MANY, 'TestTimeslots', 'test_id'),
        'userTests' => array(self::HAS_MANY, 'UserTest', 'test_id'),
    );


    // User
    return array(
        'userLanguages' => array(self::HAS_MANY, 'UserLanguage', 'user_id'),
        'userTests' => array(self::HAS_MANY, 'UserTest', 'user_id'),
    );

    // Status
    return array(
        'tests' => array(self::HAS_MANY, 'Test', 'status_id'),
        'userTests' => array(self::HAS_MANY, 'UserTest', 'status_id'),
    );

更新:我要生成的sql是:

        SELECT * FROM user_test AS UserTest
           INNER JOIN user ON user.id=UserTest.user_id
           INNER JOIN test on test.id=UserTest.test_id
           INNER JOIN (SELECT status.value, name from status where  status.tablename='user_test') AS Status ON (Status.value = UserTest.status_id)

2 个答案:

答案 0 :(得分:0)

在比较部分之前添加

$criteria->addCondition('Where a = b'); 

http://www.yiiframework.com/doc/api/1.1/CDbCriteria#addCondition-detail

需要过载和/或类似

 $criteria->addCondition('Where a = b', AND); 

答案 1 :(得分:0)

$criteria=new CDbCriteria;
$criteria->condition="user_name='john' and user_status='active'";