kitune
(Shinkitune)
November 28, 2012, 12:13pm
1
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.
robregonm
(Ricobregon)
November 28, 2012, 12:17pm
2
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.
robregonm
(Ricobregon)
November 28, 2012, 12:18pm
3
By the way, there’s no need to “‘select’ => ‘*’”, because that’s the default value.
madand
(Dev Madand)
November 28, 2012, 12:25pm
4
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();
kitune
(Shinkitune)
November 28, 2012, 12:37pm
5
robregonm:
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.
robregonm:
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'),
madand
(Dev Madand)
November 28, 2012, 1:16pm
6
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.
robregonm
(Ricobregon)
November 28, 2012, 1:28pm
7
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.