ActiveRecord eager loading + condition problem

Hi,

I have a little problem with my ActiveRecords.

I want all tags containing an ‘A’ :

I do this :




$request = new TDbCriteria();

$request->condition = "tags.type = :type AND tags.id_ugc NOT IN (SELECT id_ugc_target FROM '.$tagRelTableName.' GROUP BY id_ugc_target) AND translations.text LIKE '%A%'";

$request->params = array(':type' => $type);


$numberOfResult = Tag::model()->with('translations' => array('alias'=>'translations'))->count($request);

$tags =  Tag::model()->with('translations' => array('alias'=>'translations'))->findAll($request);



But there is an error :




TDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'translations.text' in 'where clause'.



My SQL log are :




Apr 20 14:24:33 [Debug] [System.Testing.Data.TDbCommand] Querying SQL: SELECT COUNT(DISTINCT `tags`.`id`) FROM `tags`  LEFT OUTER JOIN `tag_i18n` translations ON (translations.`id_tag`=`tags`.`id`) AND (translations.locales LIKE '%fr_FR%') WHERE (tags.type = :type AND tags.id_ugc NOT IN (SELECT id_ugc_target FROM tag_relations GROUP BY id_ugc_target) AND translations.text LIKE '%a%') (line 308, D:\Workspace\myQuizz\trunk\server\protected\lib\prado-lib\Testing\Data\TDbCommand.php)


Apr 20 14:24:33 [Debug] [System.Testing.Data.TDbCommand] Querying SQL: SELECT `tags`.`id` AS `t0_c0`, `tags`.`type` AS `t0_c1`, `tags`.`id_ugc` AS `t0_c2` FROM `tags`  WHERE (tags.type = :type AND tags.id_ugc NOT IN (SELECT id_ugc_target FROM tag_relations GROUP BY id_ugc_target) AND translations.text LIKE '%a%') LIMIT 20 (line 308, D:\Workspace\myQuizz\trunk\server\protected\lib\prado-lib\Testing\Data\TDbCommand.php)



I don’t understand, why on the second request the ActiveRecord doesn’t include translations relation ?

Of course withtout “translations.text LIKE ‘%A%’” it works.

Thx for your help.

ML

Does it work if you add together()?




$tags =  Tag::model()->with(...)->together()->findAll($request);



/Tommy

I have identified precisely the source of the bug :

If $request (CDbCriteria) don’t have limit or offset property set … it works fine.

But if id do :




$request->offset = 0;

$request->limit = 20;

$numberOfResult = Tag::model()->with($withs)->count($request);

$tags =  Tag::model()->with($withs)->findAll($request);



It doesn’t work for ‘findAll’ : the sql query is splitted into two request but the condition only works on one request.

Sounds familiar. You should read this

http://www.yiiframework.com/doc/api/CActiveFinder#together-detail

/Tommy

Ok, it’s work fine with ‘together()’

But i think strange that this doesn’t work :


$request->offset = 0;

$request->limit = 20;


Tag::model()->with(array('translations' => array('together'=>true), 

'someThing' => array('alias'=>'something')))->findAll($request);

Together method and together flag have not the same function ?