简体   繁体   中英

Unbind HasAndBelongsToMany in CakePHP 2 doesn't unbind the model

How to unbind HasAndBelongsToMany relationship in CakePHP 2.8? I have this model that is connected to Task model (N:M relationship).

class Date extends AppModel
{

    public $name = 'Date';
    public $displayField = 'rdate';
    public $actsAs = array('Containable');
    public $hasAndBelongsToMany = array('Task' => array('className' => 'Task'));

    public function getCurrentDate()
    {
        return $this->find('first',
            array(
                'conditions' => array(
                    'Date.rdate' => date('Y-m-d')
                )
            )
        );
    }

}

I would like to unbind in getCurrentDate() function this relationship, but $this->recursive = -1; in this function nor in find() doesn't work. I need only one record from Date model but find returns all Tasks in relationship with this model.

Edit#1: Even unbinding model on the fly doesn't work:

$this->unbindModel(
    array('hasAndBelongsToMany' => array('Task'))
);

still returns associated model data.

You can unbind models on the fly using unbindModel() . Check out these docs for the specifics.

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#creating-and-destroying-associations-on-the-fly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM