Please Help With Relations

Hi there,

I have 3 tables :

1-Language(id, name).



How i can search for language, in view/file/index.?

What i have tried and , it is not working:


public function relations() {

.return array(


'originalFile' => array(self::BELONGS_TO, 'OriginalFile', 'original_file_id'),

'language'=>array(self::HAS_ONE, 'Language', array('language_id'), 'through' => 'originalFile'),



 public function search(){


 $criteria->compare('', $this->language_search, true);



$this->Widget('bootstrap.widgets.TbGridView', array(


$model->relatedLinkGridColumn('language_search', 'language', 'Language', 'language'),



hi m.dhouibi,

i think you must set the relation in 2 model : file and originalfile.

set relation originalfileid in file model,

set relation id_language in originalfile model.

Hello d135ks,

i have already in originalfile

'language' => array(self::BELONGS_TO, 'Language', 'language_id'),

it still not working , really cant see the problem :huh:

can u post your full code?

and can u explain more u want to search language to get what data.

An original file has a language,

a file has an original file : in the table file(there is fiels "original_file_is" as a foregn key) so i want to show language in the view of a file

in model file.php

add :

  • public $id_language


  • ‘originalFile’ => array(self::BELONGS_TO, ‘OriginalFile’, ‘original_file_id’),

  • ‘language’=>array(self::HAS_ONE, ‘Language’, array(‘language_id’), ‘through’ => ‘originalFile’),

add this in function search() :

  • $criteria->with = array(‘language’);

  • $criteria->compare(‘’,$this->id_language,true);

in cgridview

add :





in model originalFile : add this in relation :


Thank you again for the answer, I am sure its should work, everywhere else, but on my application :)

Still got a problem with the query after I have done all the relations needed as you wrote.

CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘LIKE ‘ý%’))’ at line 1. The SQL statement executed was: SELECT COUNT(DISTINCT FROM file t LEFT OUTER JOIN stage stage ON ( LEFT OUTER JOIN workflow workflow ON ( LEFT OUTER JOIN user user ON ( LEFT OUTER JOIN original_file originalFile ON ( LEFT OUTER JOIN language language ON ( WHERE (( AND ( LIKE :ycp0))

By the way:on the view, I am using:

$this->Widget('bootstrap.widgets.TbGridView', array(


$model->relatedLinkGridColumn('id_language', 'language', 'Language', 'language'),


where 'id_language its the public $id_language in the model file.php

Any idea?

thank you

it works now :)




 'originalFile' => array(self::BELONGS_TO, 'OriginalFile', 'original_file_id'),




 $criteria->compare('', $this->language_search, false);



$this->Widget('bootstrap.widgets.TbGridView', array(...


  $model->relatedLinkGridColumnDropDownFilter('language_search', 'originalFile->language', 'Language', 'language','language','view')

Hope this would be helpful