droxy80248 2013-11-18 19:02
浏览 34
已采纳

插入CakePHP中的多个表

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条回答 默认 最新

  • dousuo8400 2013-11-18 19:23
    关注

    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'
            )
        )
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应