CakePHP3:如何在beforeDelete()回调中关联?

时间:2016-05-08 09:43:01

标签: cakephp associations cakephp-3.x

beforeDelete()回调中,我需要关联中包含的信息,但我找不到如何获取它。我的回调如下AgpoiaudiosTable.php:

public function beforeDelete($event, $entity, $options) {

    $agpoiaudio = $this->Agpoiaudios->get($entity->id, [
            'contain' => 'Agpois.AgThemes.Sites'
    ]);

    if ($agpoiaudio != null) {
        ...
        return true;
    }

    return false;
}

$this->Agpoiaudios->get()导致Cake向我发送以下错误:

Error: Table "App\Model\Table\AgpoiaudiosTable" is not associated with "Agpoiaudio"

在调用$this->Agpoiaudios->delete()之前我是否真的需要加载关联才能在$entity中找到它?

1 个答案:

答案 0 :(得分:0)

不,在调用Table::delete()之前,您不必加载相关数据,这不是错误所指示的内容。 指示的是AgpoiaudioAgpoiaudio无关,即您无法在$this->Agpoiaudio的实例中访问Agpoiaudio

因此,既然您已经在Agpoiaudio的实例中,请直接致电Table::get(),即

$agpoiaudio = $this->get($entity->id, [
    'contain' => 'Agpois.AgThemes.Sites'
]);
// ...