I think the reason is that with(‘something’) returns a CActiveFinder instance (puts actual relational query into the query string), were a scope only merges the options like condition, limit or with. So with scopes only, it’s obviously impossible to do eager loading.
public function __call($name,$parameters)
{
...
$scopes=$this->scopes();
if(isset($scopes[$name]))
{
$this->getDbCriteria()->mergeWith($scopes[$name]);
return $this;
}
...
}
public function with()
{
if(func_num_args()>0)
{
$with=func_get_args();
if(is_array($with[0])) // the parameter is given as an array
$with=$with[0];
return new CActiveFinder($this,$with);
}
else
return $this;
}
Maybe it’s possible to modify __call function to check for with option?
I had a talk with mike today and he said, that the main problem is that there could be multiple aliases for the same table and that this would result in performance loss or broken SQL queries … as far as I an understand this issue, I am not that deep into SQL.
Hmm I see, thanks for info. Maybe some core dev can look into this (again), if it’s really not possible okay, but if it’s possible in some way I would like to see this in the future. Or at least let us know how to do it if you think it can not find the way into core due to possible problems.
$models = PiiHtml::model()->localized()->checkAccess()->with('PiiInfo')->findAll($criteria);
Querying SQL: SELECT `t`.`id` AS `t0_c0`, `t`.`name` AS `t0_c1`, `t`.`html`
AS `t0_c2`, `t`.`piiInfoId` AS `t0_c3`, `PiiInfo`.`id` AS `t1_c0`,
`PiiInfo`.`model` AS `t1_c1`, `PiiInfo`.`modelId` AS `t1_c2`,
`PiiInfo`.`language` AS `t1_c3`, `PiiInfo`.`status` AS `t1_c4`,
`PiiInfo`.`type` AS `t1_c5`, `PiiInfo`.`checkAccess` AS `t1_c6`,
`PiiInfo`.`createdBy` AS `t1_c7`, `PiiInfo`.`createdAt` AS `t1_c8`,
`PiiInfo`.`modifiedBy` AS `t1_c9`, `PiiInfo`.`modifiedAt` AS `t1_c10`,
`PiiInfo`.`begin` AS `t1_c11`, `PiiInfo`.`end` AS `t1_c12`,
`PiiInfo`.`keywords` AS `t1_c13`, `PiiInfo`.`customData` AS `t1_c14`,
`PiiInfo`.`parentId` AS `t1_c15` FROM `PiiHtml` `t` LEFT OUTER JOIN
`PiiInfo` `PiiInfo` ON (`PiiInfo`.`modelId`=`t`.`id`) WHERE
(((PiiInfo.language = "de_de" or PiiInfo.language IS NULL) AND
(PiiInfo.checkAccess IS NULL )) AND (begin < NOW() AND type = 'blog')) AND
(PiiInfo.model = "PiiHtml") ORDER BY begin DESC LIMIT 2
Not:
$models = PiiHtml::model()->localized()->checkAccess()->findAll($criteria);
system.db.CDbCommand
Querying SQL: SELECT * FROM `PiiHtml` `t` WHERE ((PiiInfo.language =
"de_de" or PiiInfo.language IS NULL) AND (PiiInfo.checkAccess IS NULL ))
AND (begin < NOW() AND type = 'blog') ORDER BY begin DESC LIMIT 2
21:24:41.42943
error
system.db.CDbCommand
Error in querying SQL: SELECT * FROM `PiiHtml` `t` WHERE ((PiiInfo.language
= "de_de" or PiiInfo.language IS NULL) AND (PiiInfo.checkAccess IS NULL ))
AND (begin < NOW() AND type = 'blog') ORDER BY begin DESC LIMIT 2