What's The Difference With "with"

Hi! I have 3 tables: Provincias, Empresas, EmpresasEstilos.

In model Provincias I have a relations: empresas, empresasEstilos.

What’s the difference with:


$provincias=Provincias::model()->with(array(

					  'empresas' => array(

					    'select' => '*',

					    'with' => array(

					      'empresasEstilos' => array(

					         'select' => '*',

					      ),

					    ),

					  ),

					))->findAll();

and


$provincias=Provincias::model()->with('empresas', 'empresasEstilos')->findAll();

Not show the same results.

Big difference. Assuming that the relations are as follow:

Provincias -> (parent of) Empresas -> (parent of) EmpresasEstilos

Then,The first statement will load correctly and the second might fail (you say that the results are different, so that means that the assumed E-R is not as shown above)… in order to be equivalent (with the assumptions above) the second statement should be:


$provincias=Provincias::model()->with('empresas', 'empresas.empresasEstilos')->findAll();

Notice "empresas."

Hope this helps.

By the way, there’s no need to “‘select’ => ‘*’”, because that’s the default value.

By the way there is also no need in ‘empresas’, as ‘empresas.empresasEstilos’ implies it.

So the statement might look like:




$provincias=Provincias::model()->with('empresas.empresasEstilos')->findAll();



ok, thanks! it works with "empresas.empresasEstilos".

But I don’t understan why only “empresasEstilos” not works. empresasEstilos is a “through” relation.

In Provincias model:


'empresas'=>array(self::HAS_MANY,'Empresas','id_provincia'),			 

'empresasEstilos'=>array(self::HAS_MANY,'EmpresasEstilos','id_empresa','through'=>'empresas'),

I believe that the reason of this is the following code from CActiveFinder class:




...

if(!empty($relation->through))

{

	$slave=$this->buildJoinTree($parent,$relation->through,array('select'=>false));

	$slave->master=$element;

	$element->slave=$slave;

}

...



Looks like the intermediate table defined by trough are intended to be used only just like a bridge, and no data is actually fetched. In you case we also need data from the table POSTS (for counting posts), though the trough feature isn’t suitable for such a case. We should rather use cascaded with, which actually have solved the issue.

I’ve used “through” only for gathering foreign records when a model has been already loaded, but not for this case using findAll…

So, I assume MadAnd is right.