0
votes

I'm new to CakePHP. Now I'm struggeling with a problem I can't find an answer for on the internet for the past hour. I'm trying to save the following array to my database:

array(
    'Product' => array(
        'product_id' => '77adfe7754esda71r1999431',
        'supplier_id' => 'zalando_it',
        'product_name' => 'Sweater',
        'price_new' => '68.97',
        'affiliate_url' => 'http://example.com'
    ),
    'Image' => array(
        'image' => '99988_DP.jpg'
    )
)

Now this is an example of my ERD: https://www.dropbox.com/s/1zvfqfxes53gibb/example.png

I connected the tables correctly in the Models (by using belongsTo and hasMany variables), but still I think I'm doing something wrong.... Cause when I use the function $this->Supplier->saveAssociated($data) in the Model "Feed" it doesn't save anything. And when I use $this->Supplier->Product->saveAssociated($data) it saves the Product and it creates records in the Image table with the product_id but leaves the "image" field empty.

Only if I use $this->Supplier->Product->Image->saveAssociated($data) it saves everything correctly. But isn't that wrong? Cause to my opinion it doesn't seem right to go through each model ($this->Supplier->Product->Image...) to save all tables.... Or am I wrong?

1
did you try $this->Supplier->saveAssociated($data, array('deep' => true));Ayo Akinyemi

1 Answers

1
votes

Assuming Product hasMany Image, your data needs to be like this (notice "Image" is numbered, so there can be more than one):

array(
    'Product' => array(
        'product_id' => '77adfe7754esda71r1999431',
        'supplier_id' => 'zalando_it',
        'product_name' => 'Sweater',
        'price_new' => '68.97',
        'affiliate_url' => 'http://example.com'
    ),
    'Image' => array(
        0 => array(
            'image' => '99988_DP.jpg'
        )
    )
)