gpt4 book ai didi

php - CakePHP 2.9 连接查询返回错误结果

转载 作者:可可西里 更新时间:2023-11-01 08:38:30 24 4
gpt4 key购买 nike

我正在使用以下(有效的)MySQL 查询来获取 collection_keyword 的所有 collection_items 的计数

SELECT ck.id
, ck.working_title
, count(*) as matches
FROM collection_keywords ck
JOIN collection_keywords_collection_items ckci
ON ckci.collection_keyword_id = ck.id
JOIN nl_collection_items nci
ON nci.id = ckci.collection_item_id
GROUP
BY ck.id

SQL fiddle :http://sqlfiddle.com/#!9/7b4b0a/1

返回以下信息

+----+---------------+---------+
| id | working_title | matches |
+----+---------------+---------+
| 1 | First Title | 5 |
| 2 | Second Title | 10 |
| 3 | Third Title | 15 |
+----+---------------+---------+

我正在尝试使用 CakePHP 2.9 复制此查询,但使用 join 方法

结果返回空

这是我的 CakePHP 代码

$collectionKeywords = $this->CollectionKeyword->find('all', array(
'contain' => array(),
'fields' => array('CollectionKeyword.id, CollectionKeyword.working_title', 'count(*) AS matches'),
'joins' => array(
array(
'table' => 'collection_keywords_collection_items',
'type' => 'INNER',
'conditions' => array(
'collection_keywords_collection_items.collection_keyword_id' => 'CollectionKeyword.id'
)
)
),
'group' => 'CollectionKeyword.id',
));

关系定义如下

class CollectionKeyword extends PluginCollectionKeyword
{
public $hasAndBelongsToMany = array(
'CollectionItem' => array(
'className' => 'CollectionItem',
'joinTable' => 'collection_keywords_collection_items',
'foreignKey' => 'collection_keyword_id',
'associationForeignKey' => 'collection_item_id',
)
);
}

与 CollectionItem 的关系

class CollectionItem extends PluginCollectionItem
{

public $hasAndBelongsToMany = array(
'CollectionKeyword' => array(
'className' => 'CollectionKeyword',
'joinTable' => 'collection_keywords_collection_items',
'foreignKey' => 'collection_item_id',
'associationForeignKey' => 'collection_keyword_id',
),

);
}

最佳答案

我在您的代码中发现的一些问题:

  1. Join 的数组键应该是joins。您已经使用了join

  2. 缺少第二个内部连接的条件。

这是有效的代码。

 $collectionKeywords = $this->CollectionKeyword->find('all', array(
'contain' => array(),
'fields' => array('CollectionKeyword.id, CollectionKeyword.working_title', 'count(*) AS matches'),
'recursive' => -1,
'joins' => array(
array(
'table' => 'collection_keywords_collection_items',
'type' => 'INNER',
'conditions' => array(
"collection_keywords_collection_items.collection_keyword_id = CollectionKeyword`.id"
)
),
array(
'table' => 'collection_items',
'type' => 'INNER',
'conditions' => array(
"collection_items.id = collection_keywords_collection_items.collection_item_id"
)
),
),
'group' => 'CollectionKeyword.id',
));

它产生下面的查询

SELECT CollectionKeyword.id, CollectionKeyword.working_title, count(*) AS matches FROM `collection_keywords` AS `CollectionKeyword` INNER JOIN `collection_keywords_collection_items` ON (`collection_keywords_collection_items`.`collection_keyword_id` = `CollectionKeyword`.`id`) INNER JOIN `collection_items` ON (`collection_items`.`id` = `collection_keywords_collection_items`.`collection_item_id`) WHERE 1 = 1 GROUP BY `CollectionKeyword`.`id`

根据您使用的 Cakephp 版本,您可以使用 code 验证运行的最后一个查询。 .

关于php - CakePHP 2.9 连接查询返回错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57568304/

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