gpt4 book ai didi

php - 如何使用 CakePHP 嵌套连接?

转载 作者:可可西里 更新时间:2023-11-01 13:27:13 27 4
gpt4 key购买 nike

我正在努力表现。因此,不要使用以下 SQL 语法:

select *
from tableA INNER JOIN
tableB on tableA.id = tableB.tableA_id LEFT OUTER JOIN
( tableC INNER JOIN tableD on tableC.tableD_id = tableD.id)
on tableC.tableA_id = tableA.id

我想使用 CakePHP model->find()。这将使我也可以使用 Paginator,因为据我所知,它不适用于自定义 SQL 查询(除非您将一个分页查询硬编码到模型中,这对我来说似乎有点不灵活)。

到目前为止我尝试了什么:

/* inside tableA_controller.php, inside an action, e.g. "view" */
$this->paginate['recursive'] = -1; # suppress model associations for now
$this->paginate['joins'] = array(
array(
'table' => 'tableB',
'alias' => 'TableB',
'type' => 'inner',
'conditions' => 'TableB.tableA_id = TableA.id',
),
array(
'table' => 'tableC',
'alias' => 'TableC',
'type' => 'left',
'conditions' => 'TableC.tableA_id = TableA.id',
'joins' = array( # this would be the obvious way to do it, but doesn't work
array(
'table' => 'tableD',
'alias' => 'TableD',
'type' => 'inner',
'conditions' => 'TableC.tableD_id = TableD.id'
)
)
)
)

也就是说,将连接嵌套到结构中。但这不起作用(CakePHP 只是忽略了嵌套的 'joins' 元素,这正是我所期望的,但很遗憾。

我在评论中看到了关于如何使用语句生成器进行子查询(在 where 子句中)的提示。这里可以使用类似的技巧吗?

最佳答案

事实证明你做不到。至少不是上面提供的语法,也不是 CakePHP 1.2.6。我查看了源代码(耶!开源框架!)并找到了包含连接代码的文件 cake/libs/model/datasources/dbo_source.php

一切都从 DboSource::renderStatement() 开始,它浅游遍了 $query['joins'] 数组,用 SQL 片段替换了那些连接定义通过 DboSource::buildJoinStatement($join),它会对参数进行一些整理(填充空白等),然后调用 DboSource::renderJoinStatement 来创建 SQL单个连接子句的片段。

me: That should be easy to fix!

我被告知不要编辑 cake/libs 中的内容,所以我将文件 dbo_source.php 复制到 app/models/datasources/ 进行编辑。然后我拿起斧头,将 DboSource::renderStatement() 中的 $query['joins'] 数组的浅游重构为一个新方法 DboSource: :buildJoinStatementArray() 导致这两个方法:

function buildStatement($query, $model) {
$query = array_merge(array('offset' => null, 'joins' => array()), $query);

# refactored (extract method) to make recursion easier
$query['joins'] = $this->buildJoinStatementArray($query['joins']);

return $this->renderStatement('select', array(
'conditions' => $this->conditions($query['conditions'], true, true, $model),
'fields' => implode(', ', $query['fields']),
'table' => $query['table'],
'alias' => $this->alias . $this->name($query['alias']),
'order' => $this->order($query['order']),
'limit' => $this->limit($query['limit'], $query['offset']),
'joins' => implode(' ', $query['joins']),
'group' => $this->group($query['group'])
));
}
/**
* Replaces the join statement array syntax with SQL join clauses.
*/
function buildJoinStatementArray($joins) {
if (!empty($joins)) {
$count = count($joins);
for ($i = 0; $i < $count; $i++) {
if (is_array($joins[$i])) {
$joins[$i] = $this->buildJoinStatement($joins[$i]); # $joins[$i] now contains something like "LEFT JOIN users As User on User.group_id = Group.id"
}
}
}
return $joins;
}

一旦我有了 DboSource::buildJoinStatementArray(),就该更改 DboSource::buildJoinStatement() - 我所做的只是添加了对 的检查$data['joins'] 以及针对该情况的替代呈现方法:

function buildJoinStatement($join) {
$data = array_merge(array(
'type' => null,
'alias' => null,
'table' => 'join_table',
'conditions' => array()
), $join);

if (!empty($data['alias'])) {
$data['alias'] = $this->alias . $this->name($data['alias']);
}
if (!empty($data['conditions'])) {
$data['conditions'] = trim($this->conditions($data['conditions'], true, false));
}

# allow for nested joins
if (!empty($data['joins']) and is_array($data['joins'])) {
$data['joins'] = $this->buildJoinStatementArray($data['joins']);
return $this->renderNestedJoinStatement($data);
}
else
{
return $this->renderJoinStatement($data);
}
}

新的 renderNestedJoinStatement() 方法与 DboSource::renderJoinStatement() 非常相似:

/**
* Renders a final SQL JOIN that contains nested join statements
*
* @param array $data
* @return string
*/
function renderNestedJoinStatement($data) {
extract($data);
$nestedJoins = implode(' ', $joins);
return trim("{$type} JOIN ({$table} {$alias} {$nestedJoins})ON ({$conditions})");
}

关于php - 如何使用 CakePHP 嵌套连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2779289/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com