Php PHQL“;其中xxx在()中;只能获取1个数据

Php PHQL“;其中xxx在()中;只能获取1个数据,php,mysql,phalcon,Php,Mysql,Phalcon,我正在用Phalcon创建一个RESTful API。 我想通过ID从“Shop”表中获取一些数据。 PHP代码: $app->post('/api/shops/search', function () use ($app) { $ids_shop = $app->request->getJsonRawBody(); $ids = array(); foreach($ids_shop as $id){ $ids[] = $id-&

我正在用Phalcon创建一个RESTful API。
我想通过ID从“Shop”表中获取一些数据。
PHP代码:

$app->post('/api/shops/search', function () use ($app) {
    $ids_shop = $app->request->getJsonRawBody();
    $ids = array();
    foreach($ids_shop as $id){
            $ids[] = $id->id_shop;
    }
    $ids_str = implode(",", $ids);
    $shops = getShopsData($ids_str, $app);

    return $shops;
});

function getShopsData($ids_shop, $app) {
    $phql = "SELECT * FROM Shops WHERE Shops.id IN ( :ids: )";
    $shops = $app->modelsManager->executeQuery($phql, array(
        'ids' => $ids_shop
    ));
    return $shops;
}
测试:

但是,我只能获得一个id为1的数据。我也试过:

curl -i -X POST -d '[{"id_shop":2},{"id_shop":1}]' http://localhost/sample/api/shops/search
这将返回一个id为2的数据


为什么我不能获取多个数据?我的日志上写着$ids_str=“1,2”,所以我认为phql查询可能是正确的…

因为很少有同时使用PDO和phql的示例,并且适合您的示例,在Phalcon queryBuilder机制中还有一种可能的方法:

$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) // <<< it is an array!
        ->betweenWhere('traffic_date', self::$daterange['from'], self::$daterange['to']);

由于使用PDO和PHQL的示例很少,并且适合您的示例,因此在Phalcon queryBuilder机制中还有一种可能的方法:

$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) // <<< it is an array!
        ->betweenWhere('traffic_date', self::$daterange['from'], self::$daterange['to']);

我试过了,但是没有效果。你的绑定参数是
:id:
-这是打字错误吗?您不需要尾随冒号,因此它应该是
:ids
.Thanx,但我已经尝试过了,但没有效果。好吧;你有没有试着按照Barmar找到的重复代码修改代码?如果您有,请编辑您的问题,并包括您现在使用的代码?重新打开,因为从评论来看,似乎有针对该问题的特定于phalcon的解决方案,但在dupe中没有指定。我在那里尝试过,但没有效果。您的绑定参数是
:ids:
-这是打字错误吗?您不需要尾随冒号,因此它应该是
:ids
.Thanx,但我已经尝试过了,但没有效果。好吧;你有没有试着按照Barmar找到的重复代码修改代码?如果您有,请编辑您的问题,并包括您现在使用的代码?重新打开,因为从评论来看,似乎有针对该问题的特定于phalcon的解决方案,而dupe中没有指定这些解决方案。
$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' AND '%s' ",
        join(',', self::$keywords), self::$daterange['from'], self::$daterange['to']);

$results = new \Phalcon\Mvc\Model\Resultset\Simple(null, $realModel, $realModel->getReadConnection()->query($sql));