CakePHP cake saveall 不保存关系模型

CakePHP cake saveall not saving relation models

我正在尝试创建一个对象,该对象具有保存 Product、其 n 变体及其 n 图像所需的所有字段。但由于某种原因,saveAll() 无法正常工作。

我尝试保存一系列产品及其变体和图像,但没有成功。它也只保存了产品。我确实在模型中设置了关系

Product.php:

 public $hasMany = array(
    'product_variant' => array(
        'className' => 'ProductVariant',
        'foreignKey' => 'product_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    ),
    'product_image' => array(
        'className' => 'ProductImage',
        'foreignKey' => 'product_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    ),
    'product_mercadolibre' => array(
        'className' => 'ProductMercadolibre',
        'foreignKey' => 'product_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

ProductVariant.php:

//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
    'Product' => array(
        'className' => 'Product',
        'foreignKey' => 'product_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

ProductImage.php:

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Product' => array(
            'className' => 'Product',
            'foreignKey' => 'product_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

这是产品对象:

[Product] => Array
(
    [product_group_id] => 418112473
    [handle] => advocate-ct-circa
    [title] => Advocate CT Circa
    [body] => :html_body:
    [vendor] => Britax
    [type] => Car Seats
    [tags] => 0-3m, 12-18m, 18-24m, 24+m, 3-6m, 6-12m, auto asientos, britax, car seats, Dic152015, GooglePLA, hotsale-equipo, Niña, Niño, rn-18, Unisex
    [published_at] => 2015-06-24T01:02:00-05:00
    [published_scope] => global
    [option1_name] => Title
    [option1_value] => Default Title,
    [image_src] => https://cdn.shopify.com/s/files/1/0154/0015/products/circa.jpg?v=1447768099
    [ProductVariant] => Array
        (
            [variant_id] => 1096438833
            [title] => Default Title
            [option1_name] => Default Title
            [option2_name] => 
            [option3_name] => 
            [variant_sku] => E9LT95Q - E1A265Q
            [variant_grams] => 0
            [variant_inventory_tracker] => shopify
            [variant_inventory_qty] => 4
            [variant_inventory_policy] => deny
            [variant_fulfillment_service] => manual
            [variant_price] => 8999.00
            [variant_compare_at_price] => 
            [variant_requires_shipping] => 1
            [variant_taxable] => 
            [variant_barcode] => 
            [variant_image] => 1151565069
            [variant_weight_unit] => kg
        )

    [PorductImage] => Array
        (
            [variant_image_id] => 1225124
            [variant_image] => https://cdn.shopify.com/s/files/1/0154/0015/products/2780-ca4_2d26fff2-368d-4271-bd13-c344b5d08fb7.jpg?v=1447768100
        )

)

这是我用来保存产品对象的代码。

$this->Product->create();
if ($this->Product->saveAll($singleProduct)) {

} else {

    echo "Fallo guardar " . $singleProduct['handle'] . "<br>";

}

$singleProduct持有前面提到的对象。

所有这些仅保存 Product 模型,而不保存变体或图像。我在这里做错了什么? D:

编辑:

我尝试了 saveAll() 一系列产品,如下所示:

[
  {
    "Product": {
      "product_group_id": 418112473,
      "handle": "advocate-ct-circa",
      "title": "Advocate CT Circa",
      "body": "HTML-Body",
      "vendor": "Britax",
      "type": "Car Seats",
      "tags": "0-3m, 12-18m, 18-24m, 24+m, 3-6m, 6-12m, auto asientos, britax, car seats, Dic152015, GooglePLA, hotsale-equipo, Niña, Niño, rn-18, Unisex",
      "published_at": "2015-06-24T01:02:00-05:00",
      "published_scope": "global",
      "option1_name": "Title",
      "option1_value": "Default Title,",
      "image_src": "https://cdn.shopify.com/s/files/1/0154/0015/products/circa.jpg?v=1447768099"
    },
    "ProductVariant": [
      {
        "variant_id": 1096438833,
        "title": "Default Title",
        "option1_name": "Default Title",
        "option2_name": null,
        "option3_name": null,
        "variant_sku": "E9LT95Q - E1A265Q",
        "variant_grams": 0,
        "variant_inventory_tracker": "shopify",
        "variant_inventory_qty": 4,
        "variant_inventory_policy": "deny",
        "variant_fulfillment_service": "manual",
        "variant_price": "8999.00",
        "variant_compare_at_price": null,
        "variant_requires_shipping": true,
        "variant_taxable": false,
        "variant_barcode": "",
        "variant_image": 1151565069,
        "variant_weight_unit": "kg"
      }
    ],
    "ProductImage": [
      {
        "variant_image_id": 1151565069,
        "variant_image": "https://cdn.shopify.com/s/files/1/0154/0015/products/circa.jpg?v=1447768099"
      },
      .....
    ]
  },
  {
    "Product": {
      "product_group_id": 418498017,
      "handle": "advocate-ct-tahoe",
      "title": "Advocate CT Tahoe",
      "body": "HTML_BOdY",
      "vendor": "Britax",
      "type": "Car Seats",
      "tags": "0-3m, 12-18m, 18-24m, 24+m, 3-6m, 6-12m, auto asientos, britax, car seats, Dic152015, GooglePLA, hotsale-equipo, Niña, Niño, rn-18, Unisex",
      "published_at": "2015-06-24T01:02:00-05:00",
      "published_scope": "global",
      "option1_name": "Title",
      "option1_value": "Default Title,",
      "image_src": "https://cdn.shopify.com/s/files/1/0154/0015/products/tahoe.jpg?v=1447768138"
    },
    "ProductVariant": [
      {
        "variant_id": 1097451593,
        "title": "Default Title",
        "option1_name": "Default Title",
        "option2_name": null,
        "option3_name": null,
        "variant_sku": "E1A265N",
        "variant_grams": 0,
        "variant_inventory_tracker": "shopify",
        "variant_inventory_qty": 2,
        "variant_inventory_policy": "deny",
        "variant_fulfillment_service": "manual",
        "variant_price": "8999.00",
        "variant_compare_at_price": null,
        "variant_requires_shipping": true,
        "variant_taxable": false,
        "variant_barcode": "",
        "variant_image": null,
        "variant_weight_unit": "kg"
      }
    ],
    "ProductImage": [
      {
        "variant_image_id": 1152463301,
        "variant_image": "https://cdn.shopify.com/s/files/1/0154/0015/products/tahoe.jpg?v=1447768138"
      },
      .......
    ]
  }
]

我回到原点 D:它只保存产品图片。我检查了模型现在是否已更正。但它不会保存变体或图像。

对此有什么想法吗? o.o

您的数据结构不正确。为了能够保存 hasMany 数据,您必须将以下内容提供给 saveAll()

[Product] => Array
(
    [product_group_id] => 418112473
    [handle] => advocate-ct-circa
    [title] => Advocate CT Circa
    [body] => :html_body:
    [vendor] => Britax
    [type] => Car Seats
    [tags] => 0-3m, 12-18m, 18-24m, 24+m, 3-6m, 6-12m, auto asientos, britax, car seats, Dic152015, GooglePLA, hotsale-equipo, Niña, Niño, rn-18, Unisex
    [published_at] => 2015-06-24T01:02:00-05:00
    [published_scope] => global
    [option1_name] => Title
    [option1_value] => Default Title,
    [image_src] => https://cdn.shopify.com/s/files/1/0154/0015/products/circa.jpg?v=1447768099
)
[ProductVariant] => Array
( 
    Array 
    (
        [variant_id] => 1096438833
        [title] => Default Title
        [option1_name] => Default Title
        [option2_name] =>
        [option3_name] =>
        [variant_sku] => E9LT95Q - E1A265Q
        [variant_grams] => 0
        [variant_inventory_tracker] => shopify
        [variant_inventory_qty] => 4
        [variant_inventory_policy] => deny
        [variant_fulfillment_service] => manual
        [variant_price] => 8999.00
        [variant_compare_at_price] =>
        [variant_requires_shipping] => 1
        [variant_taxable] =>
        [variant_barcode] =>
        [variant_image] => 1151565069
        [variant_weight_unit] => kg
    )
)
[ProductImage] => Array
(
    Array 
    (
        [variant_image_id] => 1225124
        [variant_image] => https://cdn.shopify.com/s/files/1/0154/0015/products/2780-ca4_2d26fff2-368d-4271-bd13-c344b5d08fb7.jpg?v=1447768100
    )
)

编辑:修复关系

将您的 Product 关系替换为以下内容:

Product.php:

 public $hasMany = array(
    'ProductVariant' => array(
        'className' => 'ProductVariant',
        'foreignKey' => 'product_id',
        'dependent' => false
    ),
    'ProductImage' => array(
        'className' => 'ProductImage',
        'foreignKey' => 'product_id',
        'dependent' => false
    ),
    'ProductMercadolibre' => array(
        'className' => 'ProductMercadolibre',
        'foreignKey' => 'product_id',
        'dependent' => false
    )
);