如何设置可控行为+ cakephp2.x [英] how to set containable behavior + cakephp2.x
问题描述
>
这里是我的可包含条件
$ userStatusCondition = array('AND' => array(
'OR'=> array(
'TransactionStatus.buyer_status_id'=> $ user_status_id,
'TransactionStatus.seller_status_id'=> $ user_status_id,
)
)
)
合并条件(其他条件+ user_status条件)
$ transactions = $ this-> Transaction-> find('all',array(
'conditions'=> Set :: merge($ otherConditions, $ userStatusCondition),
'recursive'=> 2));
在TransactionModel中我设置
public $ actsAs = array('Containable');
public $ belongsTo = array(
'UserStatus'=> array(
'className'=>'UserStatus',
'foreignKey'=& 'buyer_status_id',
'conditions'=>'',
'fields'=>'',
'order'=>''
),
'UserStatus'=> array(
'className'=>'UserStatus',
'foreignKey'=>'seller_status_id',
'conditions'=> ,
'fields'=>'',
'order'=>''
),
);
问题
t喜欢使用递归2:它减少find方法 - 我如何认为可以正确我想我需要删除递归
2)我有四个user_statuses在User_Status表1)等待2)注意3)block 4)完成,我想找到被buyer_status_id或seller_status_id过滤的交易
我不知道如何创建新行为 - 有一个小文章
使用包含
<另一种链接到表关系的方法是使用bindModel强制加入。
下面是一个绑定4个表的代码项目 - >类别 - >章节 - >部门。您可以在关系范围内找到任何内容。
$ this-> unbindModel(array(
'belongsTo '=> array('Category')
));
$ this-> bindModel(array(
'hasOne'=> array(
'Category'=> array(
'foreignKey'=> ; false,
'condition'=> array('Category.id = Item.category_id')
),
'Section'=> array(
'foreignKey' => false,
'conditions'=> array('Section.id = Category.section_id')
)
'Division'=> array(
'foreignKey '=> false,
'conditions'=> array('Division.id = Section.division_id')
)
)
)
$ result = $ this-> find('first',array(
'conditions'=> array('Item.id'=> $ id),
' '=> array('Category','Section','Division'),
'fields'=> array('Division.id')
)
通过这样做,您可以自由地查询表范围内的任何内容。
此处的更多信息: http ://mark-story.com/posts/view/using-bindmodel-to-get-to-deep-relations
here is my containable condition
$userStatusCondition = array('AND' => array(
'OR'=> array(
'TransactionStatus.buyer_status_id'=>$user_status_id,
'TransactionStatus.seller_status_id'=>$user_status_id,
)
)
);
Merge conditions (other conditions + user_status condition)
$transactions = $this->Transaction->find('all',array(
'conditions'=> Set::merge($otherConditions, $userStatusCondition),
'recursive'=>2));
in TransactionModel I set the
public $actsAs = array('Containable');
public $belongsTo = array(
'UserStatus' => array(
'className' => 'UserStatus',
'foreignKey' => 'buyer_status_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'UserStatus' => array(
'className' => 'UserStatus',
'foreignKey' => 'seller_status_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
problems
1) I don't like to use recursive 2: it decreases the find method - how ever I think after containable correctly I think I need to remove recursive
2) I have four user_statuses in User_Status table 1)waiting 2)attention 3)block 4)Complete which I want to find transactions filtered by buyer_status_id or seller_status_id
I donno how to create new behavior - there is small article about it Using Contain
Another way of linking to table relationship is to use bindModel to force Join.
Below is a code that binds 4 tables Items->Categories->Sections->divisions. You can find anything within the range of the relationship.
$this->unbindModel(array(
'belongsTo' => array('Category')
));
$this->bindModel(array(
'hasOne' => array(
'Category' => array(
'foreignKey' => false,
'conditions' => array('Category.id = Item.category_id')
),
'Section' => array(
'foreignKey' => false,
'conditions' => array('Section.id = Category.section_id')
)
'Division' => array(
'foreignKey' => false,
'conditions' => array('Division.id = Section.division_id')
)
)
));
$result = $this->find('first', array(
'conditions' => array('Item.id' => $id),
'contain' => array('Category', 'Section', 'Division'),
'fields' => array('Division.id')
));
By doing this you can freely query anything in the range of the tables.
More Info here: http://mark-story.com/posts/view/using-bindmodel-to-get-to-deep-relations
这篇关于如何设置可控行为+ cakephp2.x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!