Phalcon中的嵌套查询

时间:2015-02-05 03:43:08

标签: phalcon

我有三张桌子,ER图如下所示 enter image description here

表“dNodes”中的字段“f”包含浮点数。我的第一项工作是收集“f”值为3的“dNodes”中的记录,因此该任务的phql为SELECT * FROM dNodes WHERE dNodes.f = 3

然后我想收集“邻居”的记录,其中“aInd”和“bInd”都与我的第一份作品的结果相匹配。就sql而言,我可以使用

SELECT * FROM neighbours AS n WHERE n.aInd IN (SELECT a.nodeInd FROM dNodes AS a WHERE a.f = 3) AND n.bInd IN (SELECT b.nodeInd FROM dNodes AS b WHERE b.f = 3)

SELECT n.* FROM neighbours AS n INNER JOIN (SELECT aa.nodeInd FROM dNodes AS aa WHERE aa.f = 3) AS a ON n.aInd = a.nodeInd INNER JOIN (SELECT bb.nodeInd FROM dNodes AS bb WHERE bb.f = 3) AS b ON n.bInd = b.nodeInd;

他们都在MySQL中给我相同和正确的结果。要通过Phalcon RESTful Web服务收集此类数据,我使用以下代码:

$app->get('/neighbours', function() use ($app){
$phql = "SELECT n.* FROM neighbours AS n INNER JOIN (SELECT aa.nodeInd FROM dNodes AS aa WHERE aa.f = 3) AS a ON n.aInd = a.nodeInd INNER JOIN (SELECT bb.nodeInd FROM dNodes AS bb WHERE bb.f = 3) AS b ON n.bInd = b.nodeInd;";
$neis = $app->modelsManager->executeQuery($phql);

$data = array();
foreach($neis as $nei){
    $data[] = array(
        "aInd" => $nei->aInd,
        "bInd" => $nei->bInd,
        "distance" => $nei->distance
    );
}

echo json_encode($data);
});

但最终得到了一个错误

  

致命错误:未捕获异常'Phalcon \ Mvc \ Model \ Exception'   message'语法错误,意外令牌(靠近'SELECT aa.nodeInd   FROM dNodes AS aa WHERE aa.f = 3)AS a ON n.aInd = a.nodeInd INNER   JOIN(SELECT bb.nodeInd FROM dNodes AS bb WHERE bb.f = 3)AS b ON   n.bInd = b.nodeInd;',解析时:SELECT n。* FROM FROM AS n   INNER JOIN(选择aa.nodeInd FROM dNodes AS aa WHERE aa.f = 3)AS a   ON n.aInd = a.nodeInd INNER JOIN(SELECT bb.nodeInd FROM dNodes AS bb   WHERE bb.f = 3)AS b ON n.bInd = b.nodeInd; (214)'在D:\ Program中   第10行的文件\ wamp \ www \ FYP \ api \ api_neighbours.php

1 个答案:

答案 0 :(得分:2)

据我所知,subqueries are not supported in PhQL。您可以从DI()收集连接界面,并尝试将原始查询放入其中:

//$connection = $app->getDI()->get('db');
$connection = \Phalcon\DI::getDefault()->get('db');
$neis = $connection->query($phql); // standard-SQL query