Get a list of related data

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?

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;

}

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?