gpt4 book ai didi

php - CakePHP HABTM 条件

转载 作者:行者123 更新时间:2023-11-29 23:19:16 25 4
gpt4 key购买 nike

我想对我的 HABTM 属性设定一个条件我在 CakePHP 2.x 中有以下 HABTM 关系:

练习.php

public $hasAndBelongsToMany = array(
'Attribute' => array(
'className' => 'Attribute',
'joinTable' => 'practises_attributes',
'foreignKey' => 'practise_id',
'associationForeignKey' => 'attribute_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);

属性.php

public $hasAndBelongsToMany = array(
'Practise' => array(
'className' => 'Practise',
'joinTable' => 'practises_attributes',
'foreignKey' => 'attribute_id',
'associationForeignKey' => 'practise_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);

现在我想在我的 PractiseController.php 中找到所有包含属性条件的内容

PractiseController.php

$cond['Attribute.id'] = array(1,2,3);
$this->Practise->find('all', array('conditions' => $cond));

然后我收到以下错误:

错误:SQLSTATE[42S22]:未找到列:1054“where 子句”中的未知列“Attribute.id”

SQL 查询:

选择 练习.id, 练习标题, 练习 body , FROM db.练习 AS 练习 WHERE 属性.id IN (1, 2, 3)

如何让 CakePHP 也将 HABTM 表加入到查找查询中?

最佳答案

您可以使用 contains 例如

$this->Practise->find('all', array('contain' => 'Attribute.id = whatever'));

您也可以手动加入:

$options['joins'] = array(
array('table' => 'practises_attributes',
'alias' => 'PractiseAttribute',
'type' => 'INNER',
'conditions' => array(
'Attribute.id = whatever',
)
) );

关于php - CakePHP HABTM 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27469246/

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