I'm using Yii CDBCriteria to find records.
I have to put a string value as a custom column in "SELECT" like this:
$criteria = new CDbCriteria;
$criteria->select = array('*, DATE_ADD(createdon, INTERVAL :timezonediff SECOND) as createdon' );
$criteria->condition = "some_amount > 1000";
$criteria->params = array(':timezonediff' => "10800");
$transModel = new TransactionModel;
I'm having a problem where I'm not able to provide the value for ":timezonediff" token in SELECT.
$criteria->params
is only working for the tokens in condition but not for SELECT.
I even tried to give an array in $criteria->select
, but it didn't work.
Does anyone know if I'm doing something wrong here ?
Thank you all for contributing. I have solved the issue. I was doing a mistake which was causing an error. I was providing this criteria to another object which did not have :timezonediff token.
Parameters for the tokens even in "select" can be binded in $criteria->params smoothly and it will create no error. So technically there will be no error in the code I posted in question.
Try:
$criteria->condition = $main_condition;
$criteria->params = array(':timezonediff'=>$main_condition);
You can try to get around this through a CDbExpression
:
$criteria->select=array(
'*',
new CDbExpression('DATE_ADD(`createdon`, INTERVAL :timezonediff SECOND) as createdon',array(
':timezonediff'=>10800,
)),
);