使用HABTM在find()中CakePHP多个模型条件

使用HABTM在find()中CakePHP多个模型条件,cakephp,join,has-and-belongs-to-many,Cakephp,Join,Has And Belongs To Many,我的模式具有以下关系: User hasMany Transaction belongsTo User Item hasMany Transaction belongsTo Item User hasManyAndBelongsTo Item using Transaction as join table 我想返回属于给定用户id的给定项目符号的所有项目。我能够检索到属于用户的所有项目 $this->User->find('all', array( 'conditions' =

我的模式具有以下关系:

User hasMany Transaction belongsTo User 
Item hasMany Transaction belongsTo Item
User hasManyAndBelongsTo Item using Transaction as join table 
我想返回属于给定用户id的给定项目符号的所有项目。我能够检索到属于用户的所有项目

$this->User->find('all', array( 'conditions' => 
    array( 'User.id' => $userId ) ) )
我能够用给定的项目名称检索所有项目

$this->Item->find( 'all', array( 'conditions =>
    array( 'Item.symbol' => $itemSymbol ) );
但是,如果我尝试在$this->Item->find()、$this->Item->User->find()、$this->User->find()、$this->User->find()或$this->User->Item->find()上使用条件数组('Item.symbol'=>$itemssymbol,'User.id'=>$userId),我会得到错误

SQL Error: 1054: Unknown column 'Item.id' in 'where clause'

我已经在所有版本的烹饪书中大量阅读了HABTM,并阅读了数十篇关于它的论坛/SO帖子,但我还没有幸运地提出这个问题

相关信息: //User.php关联

// a user hasMany transactions
     var $hasMany = array('Transaction' =>
      array('className' => 'Transaction',
      'order' => 'Transaction.created DESC', // order by descending time of transaction
      //'limit' => '1', // change this if we need more, for example, if we need a user transaction history
      'foreignKey' => 'user_id',
      )
      ); 
    // a user hasAndBelongsTo items through transactions
    var $hasAndBelongsToMany = array('Item' =>
        array('className' => 'Item',
            'joinTable' => 'transactions',
            'foreignKey' => 'user_id',
            'associationForeignKey' => 'item_id',
            'order' => '',
            'limit' => '',
            'unique' => true,
            'finderQuery' => '',
            'deleteQuery' => '',
        )
    );
var $hasMany = array('Transaction' =>
        array('className' => 'Transaction',
            'order' => 'Transaction.created DESC',
            //'limit' => '1', // change this if we need more, for example, if we need a item transaction history
            'foreignKey' => 'item_id',
        )
    );
    // a item hasAndBelongsTo users through transactions
    var $hasAndBelongsToMany = array('User' =>
        array('className' => 'User',
            'joinTable' => 'transactions',
            'foreignKey' => 'item_id',
            'associationForeignKey' => 'user_id',
            'order' => '',
            'limit' => '',
            'unique' => true,
            'finderQuery' => '',
            'deleteQuery' => '',
        )
    );
//Item.php关联

// a user hasMany transactions
     var $hasMany = array('Transaction' =>
      array('className' => 'Transaction',
      'order' => 'Transaction.created DESC', // order by descending time of transaction
      //'limit' => '1', // change this if we need more, for example, if we need a user transaction history
      'foreignKey' => 'user_id',
      )
      ); 
    // a user hasAndBelongsTo items through transactions
    var $hasAndBelongsToMany = array('Item' =>
        array('className' => 'Item',
            'joinTable' => 'transactions',
            'foreignKey' => 'user_id',
            'associationForeignKey' => 'item_id',
            'order' => '',
            'limit' => '',
            'unique' => true,
            'finderQuery' => '',
            'deleteQuery' => '',
        )
    );
var $hasMany = array('Transaction' =>
        array('className' => 'Transaction',
            'order' => 'Transaction.created DESC',
            //'limit' => '1', // change this if we need more, for example, if we need a item transaction history
            'foreignKey' => 'item_id',
        )
    );
    // a item hasAndBelongsTo users through transactions
    var $hasAndBelongsToMany = array('User' =>
        array('className' => 'User',
            'joinTable' => 'transactions',
            'foreignKey' => 'item_id',
            'associationForeignKey' => 'user_id',
            'order' => '',
            'limit' => '',
            'unique' => true,
            'finderQuery' => '',
            'deleteQuery' => '',
        )
    );
//Transaction.php关联:

var $belongsTo = array('User', 'Item');

你在寻找可控制的行为;e、 g

// app_model.php
var $actsAs = array(
    'Containable'
);
我假设用户扩展了AppModel,所以将为子(用户)设置Containable。现在,在控制器中,相关类的条件可以放在find()的第二个argv的'contain'键中;e、 g


Cake将找到匹配的用户并检索匹配的项目(使用相同的用户id和指定的项目.Symbol)。但在不需要时,请务必小心使用'contain'=>array(),否则Cake将检索所有已定义的关联,这将对您的数据库造成负担。

能否详细说明
(1=1)按用户id分组
以及为什么在这两种模型上都有
hasMany
和as HABTM与事务的关系?我已经删除了(1=1)按用户id分组的部分。我从一些教程中复制了它,这可能是多余的-删除它对问题或结果没有影响。用户有很多事务(反之亦然)因为我必须记录的交易数据与用户加入某个项目无关,例如交易数量、交易价格等。一个用户拥有一个项目,因为一个用户可以有许多项目,而每个项目可以有许多用户。仅注释用户和项目中的hasMany关系对e结果。仅评论用户和项目中的HABTM关系对结果也没有影响。非常感谢,Wayne!这正是我一直在寻找的信息。一旦我更好地理解它,我将编辑食谱;HABTM页面上甚至一次都没有提到Containeable!顺便问一句,AppModel是否在目录结构,还是放在models文件夹中?我最终只是将Containeable单独应用于我的每个模型。很高兴它起到了作用!AppModel将保存为app/models/app_model.php。AppController将保存为app/controllers/app_controller.php。它们没有相同的继承!当然,您可以应用Containeable individually,但这确实使维护变得更加困难!