CakePHP3 - 如何获取子元素的hasMany-Association

时间:2016-03-15 10:59:40

标签: cakephp

我有一个带有Tree Behavior的客户控制器来存储该系列:

这可以按预期工作。

例如:

id  | parent_id | type    | name
----|-----------|---------|--------------
1   | NULL      | husband | Jon Doe 
2   | 1         | wife    | Jane Doe 
3   | 1         | child   | Jim Doe 

我还有一个合同控制器,它与客户正确关联。

ContractsTable:

$this->belongsTo('Customers', [
    'foreignKey' => 'customer_id',
    'joinType' => 'INNER'
]);

CustomerTable的:

$this->belongsTo('ParentCustomers', [
    'className' => 'Customers',
    'foreignKey' => 'parent_id'
]);
$this->hasMany('ChildCustomers', [
    'className' => 'Customers',
    'foreignKey' => 'parent_id'
]);

$this->hasMany('Contracts');

客户视图()

    $customer = $this->Customers->get($id, [
        'contain' => ['ParentCustomers', 'ChildCustomers', 'Contracts']
    ]);
    debug($customer);

这对父母客户(丈夫)的预期效果如此。我得到一个像这样的对象:

object(App\Model\Entity\Customer) {
    'id' => (int) 1,
    'parent_id' => null,
    'typ' => 'husband',
    'name' => 'Jon Doe',
    'contracts' => [
     (int) 0 => object(App\Model\Entity\Contract) {
        'id' => (int) 1,
        'customer_id' => (int) 1,
        'contract_number' => '123456',
        'tariff' => 'SuperTariff ABC'
    }
    'child_customers' => [
     (int) 0 => object(App\Model\Entity\Customer) {
        'id' => (int) 2,
        'parent_id' => (int) 1,
        'typ' => 'wife',
        'name' => 'Jane Doe'
     },
      (int) 1 => object(App\Model\Entity\Customer) {
        'id' => (int) 3,
        'parent_id' => (int) 1,
        'type' => 'child',
        'name' => 'Jim Doe'
    }
}

但是儿童顾客(妻子和/或孩子)也有合同,但这些没有显示。有没有'cakephp'方式来做这件事或者我收集每个客户的数据?

提前致谢。

0 个答案:

没有答案