数据结构是:
tenants {id, ...}
contracts {id, active, tenant_id, ...}
debts {id, contract_id, ...}
所需的数据是:
租户名单,按合同和债务过滤。 条件:
请记住:
我的想法是手动创建连接,并为它们添加过滤器。但到底是怎么回事?
我的问题:
答案 0 :(得分:0)
你必须将所有这些逻辑放在模型上。 对于每个表创建一个模型(您可以使用Cake Bake工具或自己编写)。
class Tenant extends AppModel{
public $hasMany = array('Contract'); // feel free to ajust settings
}
class Contract extends AppModel{
public $belongsTo = array('Tenant');
public $hasMany = array('Debt');
}
class Debt extends AppModel{
public $belongsTo = array('Contract');
}
现在您可以在租户控制器上使用类似的东西:
function action(){
$this->Tenant->recursive = 2;
$all_contracts = $this->Tenant->find('all');
}
但是这个解决方案并不是最好的,你可以把这个逻辑包装成模型的方法。
class Tenant extends AppModel
{
public $hasMany = array('Contract');
public function contracts($is_active = true) {
/*
Create your own logic here, you have many solutions to retrieve data like
load the containable behaviors, create a custom find method, custom query ...
Do stuff here not in controller action.
*/
$this->Behaviors->load('Containable');
$this->contain(array(
'Contract' => array(
'conditions' => array(
'active' => $is_active
),
'Debt'
)
));
return $this->find('all');
}
}
class Contract extends AppModel{
public $belongsTo = array('Tenant');
public $hasMany = array('Debt');
}
class Debt extends AppModel{
public $belongsTo = array('Contract');
}
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#creating-custom-find-types