CakePHP - 将模型链接在一起

时间:2014-11-18 18:59:26

标签: cakephp

我有几个表/模型,我想在一个视图中显示模型的内容。

我的架构:

enter image description here

我的模特:

地址:

var $name = "Adress";
public $belongsTo = array("Customer", "Country");

public $hasAndBelongsToMany = array(
    'Contactperson' => array(
        'className' =>  'Contactperson'
    )
);

联系人:联系

    var $name = "Contactperson";
public $hasAndBelongsToMany = array(
        'Adress' => array(
                'className' =>  'Adress'
        )
);

国家:

var $name = "Country";
public $hasMany = "Adress";

客户:

    var $name = "Customer";
public $hasMany = array(
        'Adress' => array(
                'className' => 'Adress',
                'order' => array('Adress.mainadress DESC', 'Adress.created DESC')
        )
);

我的CustomerController:

    $customer = $this->Customer->findByid($customerId);
    $this->set('customer', $customer);

返回值是客户和地址表的内容,但我想获取每个表的内容。

我希望从客户,地址,联系人和国家/地区获取包含内容的数组。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

正确设置和链接每个表(使用外键和数据库设计)后,您可以使用CakePHP轻松检索所有相关字段。

阅读CakePHP可容纳的内容。

http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html

递归也会起作用,但如果系统变得太大,更高的递归值可能会损害您的系统。

相关问题