php - Yii1: How to use concat function in mysq like query -
i want use mysql concat function
in like expression
.
want merge firstname
, lastname
fullname
, matched results according fullname
.
have tried in yii1. below code:
$criteria = new cdbcriteria(); $criteria->select = "*"; $criteria->select = 'concat(firstname , "" , lastname) fullname'; $criteria->addcondition('fullname :match'); $criteria->params = array(':match' => $query); $models = user::model()->findall($criteria);
below generated error message:
cdbcommand failed execute sql statement: sqlstate[42s22]: column not found: 1054 unknown column 'fullname' in 'where clause' (error 500) cdbcommand::fetchcolumn() failed: sqlstate[42s22]: column not found: 1054 unknown column 'fullname' in 'where clause'. sql statement executed was: select count(*) `members` `t` fullname :match.
thanks in advance
if don't need fullname afterwards, can use concat method in clause:
$criteria = new cdbcriteria(); $criteria->addcondition('concat(userid , " " , username) :match'); $criteria->params = array(':match' => $query); $models = user::model()->findall($criteria);
however, if want keep fullname in select clause, can use alias in having clause:
$criteria = new cdbcriteria(); $criteria->select = '*, concat(userid , " " , username) fullname'; $criteria->having = 'fullname :match'; $criteria->params = array(':match' => $query); $models = user::model()->findall($criteria);
please note user model should have fullname attribute in case, otherwise won't able access fullname field.
Comments
Post a Comment