2017-08-03 74 views
0

我已經使用模型關聯加入了兩張表。這兩個表是filmsmovie_streams但是有一些錯誤如何更改Yii 1.1中的默認別名?

我的查詢:

$film = Film::model()->with('movie_streams')->find(array('select' => '*', 'condition' => 'user_id=:user_id, 'params' => array(':user_id' => $user_id))); 

Film.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 

我看到默認採用別名。對於filmstmovie_streamst0

如何設置手動別名我想爲我的上述兩個表?

回答

0

您可以通過指定關係的別名屬性來避免表別名的衝突。

$comments = Comment::model()->with(array(
    'author', 
    'post', 
    'post.author' => array('alias' => 'p_author')) 
    )->findAll(); 
0

使用

model_name.feild_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' 
)); 

,more details here

相關問題