thimt8-yii
(Thimt8 Yii)
February 16, 2011, 12:54pm
1
I have this two models:
class State
{
public function relations() {
return array(
'persons' => array(self::HAS_MANY, 'Person', 'id_state'),
);
}
}
class Person
{
public function relations() {
return array(
'idState0' => array(self::BELONGS_TO, 'State', 'id_state'),
);
}
}
I take the list of states that have persons related this way:
$statesHavePerson = State::model()->with(array('persons' => array('joinType' => 'inner join')))->findAll();
Is there a more "elegant" way?
zaccaria
(Matteo Falsitta)
February 16, 2011, 2:01pm
2
If you have the model of State, just do:
$state->persons
For retrive the people. You can also do in a cicle, but you will do lot of queries.
Therefore better to do as you did:
$states= State::model()->with(array('persons' ))->findAll();
foreach ($states as $state)
{
$state->persons;
}
thimt8-yii
(Thimt8 Yii)
February 16, 2011, 4:06pm
3
Thanks for the reply.
I want to get the list of states to use the filter on the grid of persons, as a dropdown in the column of states, listing only those states that have related persons.
This suggestion applies in this case?