首页 > 解决方案 > phalcon phql 在 where 中查找带有数组元素的查询

问题描述

我正在使用 phalcon 的查找查询,并在 where 子句中使用数组元素,但出现错误

"'3 之前的扫描错误] > :from_time:...' 解析时:SELECT..."

我有一个解决方法,不使用 phalcon 的 find,但想弄清楚如何在 where 子句中使用数组元素,欢迎任何想法。谢谢, 塔尔

phalcon(版本 3.4.1)

x86_64-pc-linux-gnu 上的 PostgreSQL 9.6.6,由 gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9) 编译,64 位

use Phalcon\Mvc\Model;
class Products extends Model
{

    public function initialize()
    {
        $this->setSource("products");
    }

    public function findByIdAndTime($id, $from_time)
    {
        $result = Products::find(["id=:id: AND create_time[3] > :from_time:",
        ['id' => $Id,'from_time' => $from_time]]);
        return $result;
    }
}

使用示例:

try
{
    $products = new Products();
    $products->findByIdAndTime(1, 1541672000);
}
catch(Exception $e)
{
    var_dump($e->getMessage());
}

postgre DB 表列 create_time 的类型为 integer[](例如 {1541600807,0,1541673916}),$from_time 的值是先前从数据库插入的 now_time()(例如 1541672000)

这是创建表的方法

CREATE TABLE public.products
(
    id int,
    create_time int[]
);

INSERT INTO products(create_time) VALUES ('{1541600807,0,1541673916}');

标签: phpfindphalcon

解决方案


尝试这个

public function findByIdAndTime($id, $from_time)
{
    $result = Products::find([
        'conditions' => 'id= :id: AND create_time[3] > :from_time:',
        'bind' => [
            'id' => $id,
            'from_time' => $from_time
        ]
    ]);

    return $result;  
}

推荐阅读