我使用以下代码:
$age=10; //user input
$select
->columns(["id"=>"id"])
->where->greaterThan("age",$age);
echo $select->getSqlString();
这给了我以下结果:
SELECT "program"."id" AS "id" FROM "program" WHERE "age" > '10'
但是,我想将>
用于整数,例如生成查询
SELECT "program"."id" AS "id" FROM "program" WHERE "age" > 10
如何使用greaterThan
谓词来实现这一目标?
我知道我也可以写->where("age > $age");
但这不安全也不美丽。
答案 0 :(得分:2)
查看the documentation,greaterThan
有此函数标题:
public function greaterThan($left, $right, $leftType = self::TYPE_IDENTIFIER, $rightType = self::TYPE_VALUE);
虽然没有记录,但将$rightType
设置为Predicate::TYPE_LITERAL
的类型可能会暴露文字整数值。我不知道TYPE_LITERAL
值是否仍在正确转义。请验证。
答案 1 :(得分:0)
看起来两个SQL语句之间没有区别:
SELECT "program"."id" AS "id" FROM "program" WHERE "age" > '10'
SELECT "program"."id" AS "id" FROM "program" WHERE "age" > 10
答案 2 :(得分:0)
两者之间似乎没什么区别。您仍然可以通过以下方式执行此操作:
$age=10; //user input
$select
->columns(["id"=>"id"])
->where->greaterThan("age",(int) $age); // type cast to int and datatype of age coloumn in database should also be taken integer
echo $select->getSqlString();