【问题标题】:Joins not used for complex HABTM search CakePhp 2连接不用于复杂的 HABTM 搜索 CakePhp 2
【发布时间】:2017-08-21 10:06:32
【问题描述】:

我有内容,它可以有属于不同标签组的标签。我有一个非常复杂的搜索条件,如下所示: 只要内容属于同一个标签组,如果它被至少一个来自搜索的标签标记,则该内容匹配。

示例: TagGroup 1 是颜色,TagGroup2 是形状。 因此,如果一个内容被标记为“蓝色”、“绿松石”和“矩形”,当我搜索“蓝色”和“矩形”时,它就会被找到 但是这个例子只是为了说明这背后的逻辑是相当复杂的。

内容 -> ContentsTag 标签组

我想开发一个搜索,对我让它工作的结果进行分页,但是在重构和框架更新之间它被破坏了。

在某些时候,我丢失了连接信息,因此我的 SQL 因缺少表而崩溃。

array(
    'limit' => (int) 10,
    'order' => array(
        'Content.objnbr' => 'asc'
    ),
    'joins' => array(
        (int) 0 => array(
            'table' => 'sang_contents_tags',
            'alias' => 'CT1', //join for the first TagGroup
            'type' => 'INNER',
            'conditions' => array(
                (int) 0 => 'CT1.content_id = Content.Id'
            )
        ),
        (int) 1 => array(
            'table' => 'sang_contents_tags',
            'alias' => 'CT2', //join for the second TagGroup
            'type' => 'INNER',
            'conditions' => array(
                (int) 0 => 'CT2.content_id = Content.Id'
            )
        )
    ),
    'conditions' => array(
        'AND' => array(
            (int) 0 => array(
                'OR' => array(
                    (int) 0 => array(
                        'CT1.tag_id' => '189' // chosen Tag 1 from the first TagGroup
                    )
                )
            ),
            (int) 1 => array(
                'OR' => array(
                    (int) 0 => array(
                        'CT2.tag_id' => '7' // chosen Tag 2 from the second TagGroup
                    )
                )
            )
        )
    ),
    'contain' => array(
        (int) 0 => 'Description',
        'ContentsTag' => array(
            'Tag' => array(
                (int) 0 => 'Taggroup'
            )
        )
    )
)

产生以下 SQL:

SELECT `Content`.`id`, `Content`.`objnbr`, `Content`.`name`, `Content`.`imagecounter`, `Content`.`videolength`, `Content`.`money_maker`, `Content`.`comment` 
FROM `my_db`.`contents` AS `Content` 
    WHERE ((`CT1`.`tag_id` = '189') AND (`CT2`.`tag_id` = '7')) 
ORDER BY `Content`.`id` DESC 
LIMIT 20 

很明显,标签 CT1 和 CT2 没有加入,我的 sql 崩溃了。

可能是容器阻止了连接吗?如果我取消设置包含,我仍然会得到相同的结果/错误。

有什么想法吗?

编辑:澄清一下,我想要实现的目标: 结果应该是这样的 SQL 语句:

SELECT `Content`.`id`, `Content`.`objnbr`, `Content`.`name`, `Content`.`imagecounter`, `Content`.`videolength`, `Content`.`money_maker`, `Content`.`comment` 
FROM
    `my_db`.`contents` AS `Content`
        INNER JOIN
    contents_tags AS CT1 ON CT1.content_id = Content.Id
        INNER JOIN
    contents_tags AS CT2 ON CT2.content_id = Content.Id
WHERE
    ((`CT1`.`tag_id` = '189')
        AND (`CT2`.`tag_id` = '7'))
ORDER BY `Content`.`id` DESC
LIMIT 10

看起来问题是由分页引起的。如果我做一个“简单”的查找,我会根据标签获得内容:

$result = $this->Content->find('all', $this->paginate['Content']);

通过查找生成的查询:

SELECT 
    `Content`.`id`,
    `Content`.`objnbr`,
    `Content`.`name`,
    `Content`.`imagecounter`,
    `Content`.`videolength`,
    `Content`.`money_maker`,
    `Content`.`comment`
FROM
    `my_db`.`contents` AS `Content`
        INNER JOIN
    `my_db`.`contents_tags` AS `CT0` ON (`CT0`.`content_id` = `Content`.`Id`)
        INNER JOIN
    `my_db`.`contents_tags` AS `CT2` ON (`CT2`.`content_id` = `Content`.`Id`)
WHERE
    ((`CT0`.`tag_id` = '56')
        AND (`CT2`.`tag_id` = '7'))
ORDER BY `Content`.`objnbr` ASC

【问题讨论】:

  • 您的查找查询是什么?
  • $result = $this->Paginator->paginate($this->modelClass, $this->param); $this->modelClass = 'Content' 和 $this->param = array( 'AND' => array( (int) 0 => array( 'OR' => array( (int) 0 => array( ' CT1.tag_id' => '189' ) ) ), (int) 1 => array( 'OR' => array( (int) 0 => array( 'CT2.tag_id' => '7' ) ) ) ) )
  • 所以看起来在此调用之前的某个地方我的信息丢失了.....这至少是进一步调试的良好开始!
  • 你是手动加入还是模型加入
  • 手动,因为我在 Tag 和 TagGroups 之间使用了相同的连接两次。标签属于标签组

标签: cakephp has-and-belongs-to-many cakephp-2.3


【解决方案1】:

我对分页类的内部进行了研究,我的结论是,它根本无法与当前的分页器一起使用,因为我无法传递这个复杂查询所需的特殊连接。

自定义查找类型也无济于事,因为我的查询太动态了。

如果有人证明我错了,我会是一个快乐的程序员。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多