E
E
EVOSandru62015-05-22 03:32:36
MySQL
EVOSandru6, 2015-05-22 03:32:36

How to properly organize a query with compare in Yii?

Hey!
What am I doing wrong?
I need to get a request like this:

SELECT COUNT(*) FROM `t_product` `t` WHERE EXIST=:ycp0 AND CAT=:ycp1 
AND
 (NAME LIKE :ycp2 OR NAME_EN LIKE :ycp3 OR NAME_EXCEL LIKE :ycp4 OR NAME_MENU LIKE :ycp5)

Tried like this
$criteria->compare('EXIST','1');
        $criteria->compare('CAT','0');
        $criteria->compare('NAME',$search->string,true,'AND');
        $criteria->compare('NAME_EN',$search->string,true,'OR');
        $criteria->compare('NAME_EXCEL',$search->string,true,'OR');
        $criteria->compare('NAME_MENU',$search->string,true,'OR');

I get:
SELECT COUNT(*) FROM `t_product` `t` WHERE (((((EXIST=:ycp0) AND (CAT=:ycp1)) AND (NAME LIKE :ycp2)) OR (NAME_EN LIKE :ycp3)) OR (NAME_EXCEL LIKE :ycp4)) OR (NAME_MENU LIKE :ycp5) Which
is correct?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
des1roer, 2015-05-22
@EVOSandru6

$connection = Yii::app()->db;
    $sql = "
               SELECT COUNT(*) FROM `t_product` `t` WHERE EXIST=:ycp0 AND CAT=:ycp1 
AND
 (NAME LIKE :ycp2 OR NAME_EN LIKE :ycp3 OR NAME_EXCEL LIKE :ycp4 OR NAME_MENU LIKE :ycp5)
                                ";
    $dataReader = $connection->createCommand($sql)->query();
    $rows = $dataReader->readAll();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question