2

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.

jnemecz
  • 3,171
  • 8
  • 41
  • 77

1 Answers1

0

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

bill
  • 1,646
  • 1
  • 18
  • 27