我使用模型关联连接了两个表。这两个表是films
和movie_streams
但是有一些错误
我的查询:
$film = Film::model()->with('movie_streams')->find(array('select' => '*', 'condition' => 'user_id=:user_id, 'params' => array(':user_id' => $user_id)));
胶片.php型号:
public function relations() {
return array(
'movie_streams' => array(self::HAS_MANY, 'MovieStream','movie_id'),
);
}
错误信息:
CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'user_id' in where clause is ambiguous
我看到默认情况下使用别名。对于films
表t
和movie_streams
表t0
如何为上述两个表设置我想要的手动别名?
可以通过指定关系的 alias 属性来避免表别名的冲突。
$comments = Comment::model()->with(array(
'author',
'post',
'post.author' => array('alias' => 'p_author'))
)->findAll();
使用
model_name.费尔德_名称
$film = Film::model()->with('movie_streams')->find(array('select' => '*', 'condition' => 'film.user_id=:user_id, 'params' => array(':user_id' => $user_id)));
或
通过使用 allias 避免表冲突
$comments=Comment::model()->with(array(
'author',
'post',
'post.author'=>array('alias'=>'p_author')))->findAll(array(
'order'=>'author.name, p_author.name, post.title'
));
,更多细节在这里