vizrs
(Ricemafersim)
September 27, 2012, 11:09am
1
Hi guys!
It is possible to use CDbExpression() on findByAttributes()? Im trying to use the following example:
$result = User::model()->findByAttributes(
array('reset_key' => $user->reset_key),
'reset_expires<=:current_date',
array(':current_date' => new CDbExpression('NOW()')));
But doing this i get the query:
SELECT * FROM `tbl_user` `t` WHERE `t`.`reset_key`='xyz' AND (reset_expires<=:current_date) LIMIT 1
and ofc, its not working. The query that im trying to do is:
SELECT * FROM `tbl_user` `t` WHERE `t`.`reset_key`='xyz' AND (reset_expires<=NOW()) LIMIT 1
Any idea?
abennouna
(Abennouna)
September 27, 2012, 12:17pm
2
Why not simply
$result = User::model()->findByAttributes(
array('reset_key' => $user->reset_key),
'reset_expires <= NOW()'
);
hemc
(Hemendra Chaudhary619)
September 27, 2012, 12:23pm
3
Do something like this
$criteria = new CDbCriteria;
$criteria->addCondition("current_date<=".now());
$result = User::model()->findByAttributes(array('reset_key' =>$user->reset_key),$criteria);
validate the format to print now, i am no sure to this.
vizrs
(Ricemafersim)
September 27, 2012, 1:53pm
4
I had already tested that option and it works, but doing that way, the value of reset_expires is not bound.
SELECT * FROM `tbl_user` `t` WHERE `t`.`reset_key`=:yp0 AND (reset_expires<=NOW()) LIMIT 1. Bound with :yp0='xyz'
So my question is, if I give a value to a variable that does not come from user input (eg, status=1, reset_expires<=NOW(), etc…) to be used in a query like the example above, do I need to worry about the bound values on query or is ok?
abennouna
(Abennouna)
September 27, 2012, 2:00pm
5
So my question is, if I give a value to a variable that does not come from user input (eg, status=1, reset_expires<=NOW(), etc…) to be used in a query like the example above, do I need to worry about the bound values on query or is ok?
No need to worry in that case imho
vizrs
(Ricemafersim)
September 27, 2012, 2:03pm
6
Ok bennouna, ty for the info!