2

如何HasAndBelongsToMany在 CakePHP 2.8 中解除绑定关系?我有这个模型连接到任务模型(N:M 关系)。

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')
                )
            )
        );
    }

}

我想在getCurrentDate()函数中解绑这种关系,但是$this->recursive = -1;在这个函数中也不行find()。我只需要模型中的一条记录,Date但 find 返回与该模型相关的所有任务。

编辑#1:即使在运行中解除绑定模型也不起作用:

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

仍然返回关联的模型数据。

4

1 回答 1

0

您可以使用unbindModel(). 查看这些文档以了解详细信息。

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

于 2016-07-19T21:56:42.623 回答