php - PHQL "WHERE xxx IN ()" can get only 1 data -
i'm creating restful api phalcon.
want data "shop" table ids.
php code:
$app->post('/api/shops/search', function () use ($app) { $ids_shop = $app->request->getjsonrawbody(); $ids = array(); foreach($ids_shop $id){ $ids[] = $id->id_shop; } $ids_str = implode(",", $ids); $shops = getshopsdata($ids_str, $app); return $shops; }); function getshopsdata($ids_shop, $app) { $phql = "select * shops shops.id in ( :ids: )"; $shops = $app->modelsmanager->executequery($phql, array( 'ids' => $ids_shop )); return $shops; }
test:
curl -i -x post -d '[{"id_shop":1},{"id_shop":2}]' http://localhost/sample/api/shops/search
however can 1 data id 1. , tried it:
curl -i -x post -d '[{"id_shop":2},{"id_shop":1}]' http://localhost/sample/api/shops/search
this returns 1 data id 2.
why cannot multiple data? log says $ids_str = "1,2", think phql query might correct...
as there few examples working both pdo , phql here , here suites example, there 1 more approach possible in phalcon querybuilder mechanism:
$builder = $this->modelsmanager->createbuilder(); $builder->addfrom('application\entities\keywordstrafficreal', 'tr') ->leftjoin('application\entities\keywords', 'kw.id = tr.keyword_id', 'kw') ->inwhere('keyword_id', self::$keywords) // <<< array! ->betweenwhere('traffic_date', self::$daterange['from'], self::$daterange['to']);
inwhere
method reachable via querybuilder far know , works same way mentioned pdo examples in (?, ?, ?)
. not in need of implementing hand.
you can skip part of creating phql , drive directly create sql query, on cost of making own validations.
$realmodel = new keywordstrafficreal(); $sql = sprintf('select * ' . 'from keywords_traffic_real tr ' . 'left join keywords kw ' . 'on kw.id = tr.keyword_id ' . 'where tr.keyword_id in(%s) ' . "and traffic_date between '%s' , '%s' ", join(',', self::$keywords), self::$daterange['from'], self::$daterange['to']); $results = new \phalcon\mvc\model\resultset\simple(null, $realmodel, $realmodel->getreadconnection()->query($sql));
Comments
Post a Comment