【问题标题】:Getting full join by cdbcriteria yii, mysql db?通过 cdbcriteria yii、mysql db 获得完全加入?
【发布时间】:2013-05-09 12:28:52
【问题描述】:

使用 Yii 的 Cdbcriteria 类如何获得完全连接(db 是 mysql)?我知道它可以通过左连接和右连接的联合来实现.....但是我没有得到它的语法

【问题讨论】:

标签: php mysql yii


【解决方案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() 方法中定义。

【讨论】:

    猜你喜欢
    • 2023-04-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
    相关资源
    最近更新 更多