2013-05-09 65 views
-1

使用Yii的Cdbcriteria類我如何獲得完全連接(db是mysql)?我知道,它可以通過工會來實現的左連接和右連接.....但它了Syntex我沒有得到通過cdbcriteria yii,mysql db完全加入?

+0

http://stackoverflow.com/q/4983864/2274209 .. – saveATcode 2013-05-09 12:33:33

+0

請幫助..........我必須使用內部搜索數據庫模型 – 2013-05-09 12:37:36

回答

-1
$criteria = new CDbCriteria; 
$criteria->with = array(
    'posts' => array(
     'joinType' => 'INNER JOIN', 
     'together' => true, 
    ), 
); 
$models = User::model()->findAll($criteria); 

foreach($models AS $model) { 
    echo $model->username;// gives you the username 
    foreach($model->posts AS $post) { 
     echo $post->title; // gives you the post title 
    } 
} 
// if it's about only one user: 
$criteria = new CDbCriteria; 
$criteria->addCondition('user_id', (int)$user_id); 
$criteria->with = array(
    'posts' => array('together' => true, 'joinType' => 'INNER JOIN'), 
); 
$model = User::model()->find($crieteria); 
echo $model->username; 
foreach ($model->posts AS $post) { 
    echo $post->title; 
} 
// also you can: 
$model = User::model()->with('posts')->findByPk((int)$user_id); 
echo $model->username; 
foreach($model->posts AS $post) { 
    echo $post->title; 
} 

在我們假設我們有有一個用戶表上面的例子: Yii AR的relations()方法定義了與帖子表和帖子關係的許多關係。

+0

你在開玩笑我,投票嗎?你這人怎麼回事? – Twisted1919 2013-05-30 23:15:54