【问题标题】:CActiveDataProvider with some criteria具有某些条件的 CActiveDataProvider
【发布时间】:2014-01-16 22:02:45
【问题描述】:

我想使用 CActiveDataProvider 从表中检索所有数据。
但我想将它加入其他表。

第一个表(成员)包括:id、account_id 等
第二个表(table2)包括:用户ID、项目名称等

member.account_id = table2.userid

我想从成员那里检索数据,但如果只有 table2.itemname = 'staff'

我怎么能意识到这一点?

    $criteria = new CDbCriteria;
    $criteria = ?????? // i don't know what to do
   $dataProvider=new CActiveDataProvider('Member', array(
        'criteria' => $criteria
));

【问题讨论】:

    标签: mysql yii


    【解决方案1】:

    默认情况下,主表将获得别名t,因此成员将被称为t

    $criteria = new CDbCriteria;
    $criteria->join = 'INNER JOIN table2 t2 ON t.account_id = t2.userid'; // define join part here
    $criteria->addCondition('t2.itemname = "staff"');
    
    $dataProvider = new CActiveDataProvider('Member', array(
        'criteria' => $criteria
    ));
    

    【讨论】:

      【解决方案2】:

      您可以在这里找到问题的解决方案:

      Using CDbCriteria in the Yii Framework - LarryUllman

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多