2014-03-12 62 views
0

如何建立在 「CActiveDataProvider」 這個查詢:與CActiveDataProvider簡單的查詢 - Yii框架

$var = word; 

    SELECT table_a.name, table_b.name 
    FROM table_a 
    LEFT JOIN table_b ON table_a.id = table_b.bid 
    WHERE table_a.name LIKE '%$var%' 

我到目前爲止這種模式:

class Table_a extends CActiveRecord { 

    public static function model($className=__CLASS__) { 
     return parent::model($className); 
    } 

    public function tableName() { 
     return Yii::app()->db->tablePrefix.'table_a'; 
    } 
} 

回答

0

您應該使用CDbCrtieria對象爲這個 請參考http://www.yiiframework.com/doc/api/1.1/CDbCriteria

在你的情況下,它將會像你的控制器一樣

<?php 
// .... Wherever you need the dataProvider, usually the action in the controller 

$criteria = new CDbCriteria(); 
$criteria->addSeachCondition('table_a.name',$var,true,"AND","LIKE"); 
$criteria->join = "LEFT OUTER JOIN table_b ON t.id = table_b.bid"; 
$dataProvider = new CActiveDataProvider('table_a',array(
    'criteria'=>$criteria 
    )); 
0

我沒有測試的代碼,但它應該必須努力:

$dataProv=new CActiveDataProvider('Table_a',array(
      'criteria'=>array(
       'select'=>"table_a.name, table_b.name", 
       'condition'=>"table_a.name LIKE '%$var%'", 
       'join'=>"LEFT JOIN table_b ON table_a.id = table_b.bid", 
     )));