Yii Framework Forum: eager loading with criteria - Yii Framework Forum

Jump to content

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

eager loading with criteria Rate Topic: -----

#1 User is offline   duplabe 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 11-August 09

Posted 21 December 2009 - 08:16 AM

Hi!

I have 2 tables: User, Status. Status belongs to User with user_id, so an user has many statuses. When I want find eagerly a user with his/her statuses with some limitation, the generated sql doesnt contain the LIMIT.

Example:

$c = new CDbCriteria;
$c->condition	= 'username=:username';
$c->params		= array(
	':username' => $username
);
$model = User::model()->with(array(
	'Statuses' => array(
		'limit'		=> self::PAGE_SIZE,
		'offset'	=> $page * self::PAGE_SIZE
	)
	))->find($c);
$this->render(
	'index',array(
        	'model'	        => $model,
		'statuses'	=> $model->Statuses
	)
);


Generated SQL:
SELECT `User`.`id` AS `t0_c0`, `User`.`username` AS `t0_c1`,
`User`.`name` AS `t0_c2`, `User`.`location` AS `t0_c3`,
`User`.`description` AS `t0_c4`, `User`.`image` AS `t0_c5`, `User`.`url` AS
`t0_c6`, `User`.`update` AS `t0_c7`, `Statuses`.`user_id` AS `t1_c0`,
`Statuses`.`id` AS `t1_c1`, `Statuses`.`banned` AS `t1_c2`,
`Statuses`.`pubdate` AS `t1_c3`, `Statuses`.`description` AS `t1_c4` FROM
`User`  LEFT OUTER JOIN `Status` `Statuses` ON
(`Statuses`.`user_id`=`User`.`id`) WHERE (username=:username) AND
(banned=0) ORDER BY Statuses.id DESC


(Order and banned = 0 condition defined in the model)

Is it a bug, or I have to use lazy loading?
0

#2 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 608
  • Joined: 08-May 09

Posted 21 December 2009 - 09:08 AM

first load the user, then his statuses

$statuses = Status::model()->ofUser($user->id)->findAll($criteria)


or without a criteria

$statuses = Status::model()->ofUser($user->id)->limit($offset, $limit)->findAll()

0

#3 User is offline   duplabe 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 11-August 09

Posted 21 December 2009 - 09:21 AM

thx!
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