0

I currently have an belongsToMany relationship between two Table, Skus and Medias. I named the join table skus_images though.

I'm here trying to save only ids, not inserting new data in an HABTM way. I have in my form :

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

And everything is working fine there, I'm correctly getting my Medias listed. But whenever I try to submit the form, I get this :

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

I've defined my relationship as such in SkusTable :

$this->belongsToMany('Images', [
     'className' => 'Media.Medias',
     'joinTable' => 'skus_images',
     'targetForeignKey' => 'image_id'
]);

The context doesn't give any insights, neither does the stack trace as it's both (almost) empty. Thanks :)

EDIT: Controller add method:

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

Controller posted data:

[
    'product_id' => '65',
    'attributes' => [
        '_ids' => ''
    ],
    'reference' => '',
    'quantity' => '420',
    'is_default' => '0',
    'images' => [
        '_ids' => [
            (int) 0 => '90'
        ]
    ]
]
yachaka
  • 5,429
  • 1
  • 23
  • 35
  • Please open a ticket, the fatal error should be prevented. Meanwhile. Can you gist the data that is getting posted to the controller and also paste the controller code, please. – José Lorenzo Rodríguez Apr 08 '15 at 07:42
  • I added the data and the controller method. I'm creating a ticket on Cake3 github. Thanks – yachaka Apr 08 '15 at 22:23

1 Answers1

0

Forgot to add the name of the association in the patchEntity associated option. Still shouldn't throw a fatal error so I created a github ticket.

yachaka
  • 5,429
  • 1
  • 23
  • 35