Cakephp HABTM关联条件导致列未找到SQL错误
在Cakephp中,当我定义Cakephp HABTM关联条件导致列未找到SQL错误,cakephp,associations,has-and-belongs-to-many,Cakephp,Associations,Has And Belongs To Many,在Cakephp中,当我定义 public $hasAndBelongsToMany = array( 'User' => array( 'className' => 'User', 'joinTable' => 'groups_users', 'foreignKey' => 'group_id', 'associationForeignKey' => 'user_id', 'u
public $hasAndBelongsToMany = array(
'User' => array(
'className' => 'User',
'joinTable' => 'groups_users',
'foreignKey' => 'group_id',
'associationForeignKey' => 'user_id',
'unique' => 'keepExisting',
'conditions' => 'User.active=1',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
设置conditions字段,以便仅从DB中获取活动用户,因此我得到错误:SQLSTATE[42S22]:Column not found:1054未知列“User.active”在“where子句”中
对于SQL查询:
SELECT `GroupsUser`.`user_id` FROM `groups_users` AS `GroupsUser` WHERE `GroupsUser`.`group_id` = 123 AND `User`.`active`=1
因为很明显,它只从联接表中获取结果。所以我在这里找到了:在对条件的描述中
条件:find()兼容条件或SQL字符串的数组。如果
如果关联表上有条件,则应使用“with”
模型,并定义其上关联的必要属性
什么是“with”模型?我将如何实现它
提前多谢 虽然这个问题已经很老了,但我最近遇到了同样的问题,我想记录我的解决方案。在上面的示例中,我没有引用用户,而是执行了一个简单的子查询,以获得如下关联字段:
public $hasAndBelongsToMany = array(
'User' => array(
'className' => 'User',
'joinTable' => 'groups_users',
'foreignKey' => 'group_id',
'associationForeignKey' => 'user_id',
'unique' => 'keepExisting',
//'conditions' => 'User.active=1',
'conditions' => array('(SELECT active FROM users WHERE users.id = GroupsUser.user_id)'=>1),
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
在我的类似示例中,现在查找并保存工作。请发布生成MySQL的代码。