Yii Framework Forum: Select query issue - Yii Framework Forum

Jump to content

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

Select query issue Rate Topic: -----

#1 User is offline   manilodisan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 17-September 09
  • Location:Bucharest

Posted 02 October 2009 - 07:32 PM

Any idea why even if I select the id it still selects everything in the query:

$role = $this->with ( 'users' )->together ()->find 
(
	array 
	(
		'select' => 'id',
		'condition' => 'user_id=:user_id',
		'params' => array ( ':user_id' => $user_id ),
		'limit' => 1,
		'order' => 'rank DESC'
	)
);


This is the generated code:
system.db.CDbCommand.query(SELECT `WB_Roles`.`id` AS `t0_c0`, users.`id` AS `t1_c0`, users.`is_active` AS `t1_c1`, users.`email` AS `t1_c2`, users.`username` AS `t1_c3`, users.`password` AS `t1_c4`, users.`confirmation_code` AS `t1_c5`, users.`date_added` AS `t1_c6` FROM `WB_Roles` LEFT OUTER JOIN `wb_user_roles` users_users ON (`WB_Roles`.`id`=users_users.`role_id`) LEFT OUTER JOIN `WB_Users` users ON (users.`id`=users_users.`user_id`) WHERE (user_id=:user_id) ORDER BY rank DESC LIMIT 1)


Maybe I'm doing something wrong but it's obvious that it extracts everything even if specified that only the id should be extracted,
0

#2 User is offline   tri 

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

Posted 02 October 2009 - 08:24 PM

View Postmanilodisan, on 02 October 2009 - 07:32 PM, said:

Any idea why even if I select the id it still selects everything in the query:


API doc says:

Quote

By default, the options specified in relations() will be used to do relational query. In order to customize the options on the fly, we should pass an array parameter to the with() method.
...
...
Post::model()->with(array(
    'author'=>array('select'=>'id, name'),
    'comments'=>array('condition'=>'approved=1', 'order'=>'createTime'),
))->findAll();



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

#3 User is offline   fhelix72 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 22-April 09
  • Location:Italy - Vicenza

Posted 07 October 2009 - 03:38 AM

View Posttri, on 02 October 2009 - 08:24 PM, said:

API doc says:


/Tommy


I have the same doubt of manilodisan, I try to specify the 'select' fields in the relation definition and also in the 'with'...->find but the same all the fields are extracted in the result array, the only utility is I can change the order of the fields extracted (so 'name, id' instead 'id, name'), but is this the scope of select?
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