Please Help With Relations

Hi there,

I have 3 tables :

1-Language(id, name).

2-originalfile(id,name,id_language).

3-file(id,name,id_originalfile)

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

What i have tried and , it is not working:

File.php


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('language.name.', $this->language_search, true);

}

view/file/index.php


$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

relations():

  • ‘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(‘language.name’,$this->id_language,true);

in cgridview

add :

array(

‘name’=>‘language’,

‘value’=>’$data->originalFile->language->name’,

),

in model originalFile : add this in relation :

‘language’=>array(self::BELONGS_TO,‘Language’,‘id_language’),

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 t.id) FROM file t LEFT OUTER JOIN stage stage ON (t.stage_id=stage.id) LEFT OUTER JOIN workflow workflow ON (stage.workflow_id=workflow.id) LEFT OUTER JOIN user user ON (t.uploaded_by=user.id) LEFT OUTER JOIN original_file originalFile ON (t.original_file_id=originalFile.id) LEFT OUTER JOIN language language ON (originalFile.language_id=language.id) WHERE ((t.active=1) AND (language.name. 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 :)

File.php


relations(){

 ....

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

 }

 search(){

 $criteria->with(originalFile.language)

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

 }

view/index.php


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

 ....

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

Hope this would be helpful