Php 通过cdbcriteria yii、mysql db获得完全加入?
使用Yii的Cdbcriteria类如何获得完全加入(db是mysql)?我知道它可以通过左连接和右连接的并集来实现……但我没有得到它的syntexPhp 通过cdbcriteria yii、mysql db获得完全加入?,php,mysql,yii,Php,Mysql,Yii,使用Yii的Cdbcriteria类如何获得完全加入(db是mysql)?我知道它可以通过左连接和右连接的并集来实现……但我没有得到它的syntex $criteria = new CDbCriteria; $criteria->with = array( 'posts' => array( 'joinType' => 'INNER JOIN', 'together' => true, ), ); $models = User:
$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;
}
在上面的例子中,我们假设我们有一个用户表,它与posts表有一:多关系,并且posts关系是在Yii AR的
relations()
方法中定义的。。请帮助……。我必须使用db模型的内部搜索