Cake 3保存belongsToMany关系崩溃

时间:2015-04-08 05:07:44

标签: php cakephp has-and-belongs-to-many cakephp-3.0

我目前在两个表SkusMedias之间有一个belongsToMany关系。我将连接表命名为skus_images。

我正在尝试仅保存ID,而不是以HABTM方式插入新数据。 我的形式是:

echo $this->Form->input('images._ids', ['options' => $images, 'multiple' => 'checkbox']);

那里的一切都很好,我正确地列出了我的媒体。 但每当我尝试提交表单时,我都会这样:

Error: Call to a member function get() on a non-object 
File /home/weshguillaume/AndyToGaby/vendor/cakephp/cakephp/src/ORM/Association/BelongsToMany.php 
Line: 874

我在SkusTable

中定义了我的关系
$this->belongsToMany('Images', [
     'className' => 'Media.Medias',
     'joinTable' => 'skus_images',
     'targetForeignKey' => 'image_id'
]);

上下文没有提供任何见解,堆栈跟踪也没有,因为它(几乎)都是空的。谢谢:))

编辑: 控制器添加方法:

public function add($product_id)
    {
        $skus = $this->Skus->newEntity();
        if ($this->request->is('post')) {
            $skus = $this->Skus->patchEntity($skus, $this->request->data(), [
                'associated' => [
                    'Attributes'
                ]
            ]);
            if ($this->Skus->save($skus)) {
                $this->Flash->success('The skus has been saved.');
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error('The skus could not be saved. Please, try again.');
            }
        }
        $attributes = $this->Skus->Attributes->find('list');
        $images = $this->Skus->Products->getMedias('list', $product_id, 'photo');
        $this->set(compact('skus', 'products', 'attributes', 'images', 'product_id'));
        $this->set('_serialize', ['skus']);
    }

控制器发布数据:

[
    'product_id' => '65',
    'attributes' => [
        '_ids' => ''
    ],
    'reference' => '',
    'quantity' => '420',
    'is_default' => '0',
    'images' => [
        '_ids' => [
            (int) 0 => '90'
        ]
    ]
]

1 个答案:

答案 0 :(得分:0)

忘记在patchEntity associated选项中添加关联的名称。仍然不应该抛出致命的错误,所以我创建了一个github票。