Yii Framework Forum: ActiveRecord eager loading + condition problem - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

ActiveRecord eager loading + condition problem Rate Topic: -----

#1 User is offline   MLKiiwy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 20-April 10

Posted 20 April 2010 - 09:42 AM

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
0

#2 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 20 April 2010 - 11:03 AM

View PostMLKiiwy, on 20 April 2010 - 09:42 AM, said:

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


Does it work if you add together()?
$tags =  Tag::model()->with(...)->together()->findAll($request);

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   MLKiiwy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 20-April 10

Posted 20 April 2010 - 11:28 AM

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.
0

#4 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 20 April 2010 - 11:37 AM

View PostMLKiiwy, on 20 April 2010 - 11:28 AM, said:

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.yiiframew...together-detail

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#5 User is offline   MLKiiwy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 20-April 10

Posted 21 April 2010 - 03:24 AM

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 ?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users