Bindmodel to binded model? Cakephp

2019-09-02 06:49发布

问题:

I bind ProductsPhoto to children using bindModel method:

$this->Category->bindModel(array
            ('hasMany' => array(
                'ProductsPhoto' => array... 

How can I bind ProductsPhoto to every product item?

Or maybe any other solution suggestion?

回答1:

in your controller ,write below code

$this->ProductsPhoto->bindModel('hasMany' => array('Product.productphoto_id' => 'ProductPhoto.id');

in your ProductPhoto model,

var $hasMany = array(
    'Product' =>
                array(
                    'className' => 'ProductPhoto',
                    'foreignKey' => 'productphoto_id',
                    'conditions' => '',
                    'fields' => '',
                    'order' => '',
                    'counterCache' => ''
            ),  

);


回答2:

On the fly: $this->ProductsPhoto->bindModel('hasMany' => 'Product');

Via ProductsPhoto class property: $hasMany = 'Product';