I’m experiencing problems getting eager loading to work as a conditional when creating a CActiveDataProvider. Here’s a simplification of the a couple tables that I have:
[indent][/indent] ------------------
[indent][/indent] | tbl_class_info |
[indent][/indent] ------------------
[indent][/indent] item_id - primary key
[indent][/indent] …various other fields…
[indent][/indent] ------------------------
[indent][/indent] | tbl_category_to_item |
[indent][/indent] ------------------------
[indent][/indent] id - primary key
[indent][/indent] category_id
[indent][/indent] item_id - relation to class_info table
I have the following relation in my ClassInfo model:
[indent][/indent] ‘category_ids’=>array(self::HAS_MANY, ‘category_to_item’, ‘item_id’)
Now, I would like to return only those rows from the tbl_class_info table that have a specific category ID contained in the tbl_category_to_item table. Each item can be part of more than one category. What I came up with is the following:
[indent][/indent] new CActiveDataProvider(
[indent][/indent][indent][/indent] ‘ClassInfo’,
[indent][/indent][indent][/indent] array(
[indent][/indent][indent][/indent][indent][/indent] ‘pagination’=>array(
[indent][/indent][indent][/indent][indent][/indent][indent][/indent] ‘pageSize’=>5,
[indent][/indent][indent][/indent][indent][/indent] ),
[indent][/indent][indent][/indent][indent][/indent] ‘criteria’=>array(
[indent][/indent][indent][/indent][indent][/indent][indent][/indent] ‘with’=>array(
[indent][/indent][indent][/indent][indent][/indent][indent][/indent][indent][/indent] ‘category_ids’=>array(
[indent][/indent][indent][/indent][indent][/indent][indent][/indent][indent][/indent][indent][/indent] ‘condition’=>‘category_ids.category_id=’.$id
[indent][/indent][indent][/indent][indent][/indent][indent][/indent][indent][/indent] )
[indent][/indent][indent][/indent][indent][/indent][indent][/indent] )
[indent][/indent][indent][/indent][indent][/indent] )
[indent][/indent][indent][/indent] )
[indent][/indent] )
Unfortunately, it appears that CActiveDataProvider ignores the eager loading altogether unless I remove the condition. It generates the following SQL statement when I turn on SQL debugging:
[indent][/indent]SELECT t
.item_id
AS t0_c0
, t
.title
AS t0_c1
,
[indent][/indent]t
.shortdesc
AS t0_c2
, t
.desc
AS t0_c3
, t
.website
AS
[indent][/indent]t0_c4
, t
.provider_id
AS t0_c5
, t
.price
AS t0_c6
,
[indent][/indent]t
.priceper
AS t0_c7
FROM tbl_class_info
t
LIMIT 5
Any idea what might be causing this?