I have a CActiveRecord named Book and Book has SELF::MANY relations with Author.
Here is the Book model :
class Book extends BangdaAR
{
public $author;
public function rules()
{
return array(
array('author', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
return array(
'authors' => array(self::HAS_MANY, 'Author', 'book_id'),
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('name',$this->author,true);
$criteria->with = array('authors'=>array('select'=>'name'));
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
}
And I have put the attribute ‘name’ in Author as a safe attribute.
Now when I search using this, there is error shown :
Error 500: <h1>CDbException</h1>
<p>CDbCommand failed to execute the SQL statement: SQLSTATE[42S22] [1054] Unknown column 'name' in 'where clause'. The SQL statement executed was: SELECT `t`.`book_id` AS `t0_c0`, `t`.`book_title` AS `t0_c1`... FROM `tbl_buku` `t` WHERE (name LIKE :ycp0) LIMIT 10
Am I implementing it right ? I follow this Search as Text Input For a Foreign Key Field in CGRidView
I dont know but it should use ‘together’ => true. But I still dont understand why and how ‘together’ works. Anyone can explain why should use ‘together’=>true ? Thanks so much
Now in you case if together is not set to true - records from table authors will joined with books using second (separate) SQL query. But in you main query you have included refernce to this table (authors.name) which apeear in WHERE clause, but JOIN statement will not appear because together is not true. This is the reason for "Unknown column …" error. Setting together to true you are forcing Yii to include JOIN statement to main SQL query - so now you can use field references from authors table[i].
Unknown column 'addedTitles.added_title' in 'where clause'. The SQL statement executed was: SELECT COUNT(DISTINCT `t`.`cd_id`) FROM `tbl_cd` `t` LEFT OUTER JOIN `tbl_imprint` `imprints` ON (`imprints`.`cd_id`=`t`.`cd_id`) WHERE (addedTitles.added_title LIKE :ycp0)
SO it seems that yii will join only the latest declared method, in this case is the tbl_imprint