model1 with model2, need only model2

$result = Model1::model()->with(‘model2’)->findAll();

If I do not need Model1 data, but only model2 data in $result, how should I change above?

(so, it means I need those model2 data which are with relation with Model1, also Model2 consists many rows, but Model1 only some)

Hi Renathy,

Remember that you also can do something like:

Model2::model()->with('model1')->findAllByAttributes(array('t.fieldInModel2'=>'anyValue', 'model1.field'=>'anotherValue'));



Model2->with(Model1) doesn’t work for me as it also returns record where there are no record in Model1 corresponding to Model2.

If I use Model1->with(Model2) it returns only record where Model2 contains related records in Model1. ANd this is what I need.

Also the question is - is not the first way too expansive or it doesn’t matter?

  1. Model1 with Model2

  2. Model2 with Model1

If Model1 contains say 10000 record, but Model2 always only 5 records.