[英]CakePHP find with LEFT JOIN and generate_series
Basically, is it possible, and if so, how do you do the following code without using the raw query() method in CakePHP 2.0.6. 基本上有可能,如果可以,那么如何在不使用CakePHP 2.0.6中的raw query()方法的情况下执行以下代码。 I am using PostgreSQL (which
generate_series()
is a function of). 我正在使用PostgreSQL(
generate_series()
是PostgreSQL的功能)。 So, how do you do this query the CakePHP way? 那么,您如何使用CakePHP方式查询呢?
$sql = "
SELECT new_rank FROM generate_series(1,999) AS new_rank
LEFT JOIN tasks ON tasks.rank = new_rank
WHERE tasks.rank IS NULL LIMIT 1
";
$data = $this->Task->query($sql);
EDIT One user said I could try to use the find call on Task and right join to generate_series(). 编辑一位用户说,我可以尝试在Task上使用find调用,然后右键连接到generate_series()。 Here is my attempt at that.
这是我的尝试。 This code throws an error, in that CakePHP is putting double quotes around the function arguments for generate_series.
该代码引发错误,因为CakePHP在generate_series的函数参数周围加上了双引号。 I wonder how I can get it to not do that?
我不知道如何才能做到这一点?
$data = $this->Task->find('all', array(
'conditions' => array('Task.rank' => null),
'limit' => 1,
'recursive' => -1,
'fields' => 'new_rank',
'joins' => array(
array(
'table'=>'generate_series(1,999)',
'alias'=>'new_rank',
'type'=>'RIGHT',
'conditions' => array(
'tasks.rank'=>'new_rank'
)
)
)
));
Which products the following SQL: 哪些产品具有以下SQL:
SELECT "Task"."new_rank" AS "Task__new_rank"
FROM "tasks" AS "Task"
RIGHT JOIN generate_series("1,999") AS "new_rank" ON ("tasks"."rank" = 'new_rank')
WHERE "Task"."rank" IS NULL LIMIT 1
What you're probably looking for is in DboSource::expression()
. 您可能正在寻找的是
DboSource::expression()
。 It basically allows you to do a SQL expression without any of Cake's escaping. 它基本上允许您执行SQL表达式而无需Cake的任何转义。 Make sure to sanitize inputs.
确保清理输入。
To have it show up as a field, you can try adding it to the fields key in your options array: 要使其显示为字段,可以尝试将其添加到options数组中的fields键中:
$ds = $this->Task->getDataSource();
$this->Task->find('all', array(
'fields' => $ds->expression('generate_series(1,999) AS new_rank')
));
If that doesn't work, you can always try DboSource::rawQuery()
which doesn't escape anything , afaik. 如果那不起作用,您可以随时尝试
DboSource::rawQuery()
,它不会转义任何内容 ,即afaik。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.