使用HABTM在find()中使用CakePHP的多个模型条件 [英] CakePHP multiple model conditions in find() with HABTM

查看:108
本文介绍了使用HABTM在find()中使用CakePHP的多个模型条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的架构具有以下关系:

My schema has the following relations:

User hasMany Transaction belongsTo User 
Item hasMany Transaction belongsTo Item
User hasManyAndBelongsTo Item using Transaction as join table 

我想返回属于给定用户ID 的给定项目符号的所有项目.我可以使用

I'd like to return all items of a given item symbol that belong to a given user id. I'm able to retrieve all the items that belong to the user with

$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上使用条件数组('Item.symbol'=> $ itemSymbol,'User.id'=> $ userId) -> User-> find(),$ this-> User-> find()或$ this-> User-> Item-> find(),我得到了错误

but if I try to use the condition array( 'Item.symbol' => $itemSymbol, 'User.id' => $userId ) on $this->Item->find(), $this->Item->User->find(), $this->User->find(), or $this->User->Item->find(), I get either the error

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

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

我已经在菜谱的所有版本中吞噬了HABTM,并阅读了数十篇有关它的论坛/SO帖子,但是我在编写此查询时没有任何运气.

I've devoured HABTM in all versions of the cookbook and read tens of forum / SO posts about it, but I haven't had any luck in forming this query.

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

Relevant Info: // User.php associations

// 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' => '',
        )
    );

//Item.php关联

// Item.php associations

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关联:

// Transaction.php associations:

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

推荐答案

您正在寻找Containable行为;例如

You're looking for the Containable behavior; e.g.

// app_model.php
var $actsAs = array(
    'Containable'
);

我假设用户扩展了AppModel,所以将为子级(用户)设置Containable.现在,在您的控制器中,相关类的条件可以放在find()的第二个argv的"contain"键中;例如

I'm assuming User extends AppModel, so Containable will be set for the child (User). Now, in your controller, conditions for the related class can be placed in the 'contain' key of the second argv of find(); e.g.

// users_controller.php
$this->User->find('first', array(
    'conditions' => array('User.id' => $userId),
    'contain' => array(
        'Item' => array(
            'conditions' => array('Item.symbol' => $itemSymbol)
        )
    )
);

Cake会找到匹配的User并检索匹配的Items(具有相同的user_id和指定的Item.Symbol.但是在不需要时,请非常小心地使用'contain'=> array(),否则Cake会检索所有定义的关联中,这会加重您的数据库负担.

Cake will find the matching User and retrieve matching Items (with the same user_id and the Item.Symbol specified. But be very careful to use 'contain' => array() when it is not required, otherwise Cake will retrieve all of the defined associations, which will tax your database.

这篇关于使用HABTM在find()中使用CakePHP的多个模型条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆