hi
i have 2 table that relation together: tl_user(sid is Primary key) $ tl_tel(id is Primary key , sid is Foreign key)
i want to Join 2table, but i can’t.
thank you
hi
i have 2 table that relation together: tl_user(sid is Primary key) $ tl_tel(id is Primary key , sid is Foreign key)
i want to Join 2table, but i can’t.
thank you
Inside the model find this function and write down their relationships:
inside both the models
let the below be inside student model:
public function relations()
{
return array(
'studentTel'=>array(self::HAS_MANY, 'Tel','sid'),
//'name of relationship'=>(self::type of relationship,'the table name that u r trying to join with','foreign key attribute')
);
}
And
let the below be inside The foreign key table model:
public function relations()
{
return array(
'studentTel'=>array(self::BELONGS_TO, 'Student','sid'),
//'name of relationship'=>(self::type of relationship,'the table name that u r trying to join with','foreign key attribute')
);
}
This is one way of defining relationship…
thank you , but want that join in Query! After you have defined relationships
suppose u have two tables: Student and Tel.
$model=Student::model()->findAll(array(
'condition' => "stuid=1",
'with'=>array('studentTel'), // the name of relationship that u write in model.
'select'=> array("*"),
));
if i am making relationship between two tables, then i usually use same relationship name…
By default "t." points to the first table and if u want to use the second table also then write "t2."
Ex: ‘condition’ => “t.stuid=1 AND t2.name=‘ABC’”,
suppose u want to display student name and Tel name in ur view. for displaying student u will use
foreach($model As $mod)
{
echo $mod['stuname'];// since this field is directly coming from student table, no need to use relationship here
echo $mod->studentTel['telname']; // since this field is in another table, so u use relationship to display it.
}
thank you very very much.
You’re Indian, you’re very kind. Mercy