2016-01-28 6 views
1

Я пытаюсь создать объект, который имеет все поля, необходимые для сохранения Product, его n вариантов и его n изображений. Но почему-то saveAll() не работает.CakePHP cake 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' => '' 
     ) 
    ); 

Вот объект Porduct:

[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:

EDIT:

Я попытался 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: Это только сохраняет изображения продукта. Ive проверил, что модели исправлены сейчас. Но это не спасет варианты или изображения.

Любые мысли по этому поводу? o.o

+0

я думаю, что, например ProductVariant должен быть связан массив этого формата 'ProductVariant' => Array (массив ('variant_id' => 1096438833)) –

+0

Спасибо @NNem за советы, как показано ниже, я попытался это но не повезло. Объект Product является единственным сохраненным. –

ответ

2

Ваши данные структурированы неправильно. Для того, чтобы быть в состоянии сохранить 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 
    ) 
) 

EDIT: Фикс Отношения

Замените 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 
    ) 
); 

См

+0

Привет, спасибо за ответ и хороший совет. Я прочитал разделы saveAssociated и saveAll и смоделировал объект после примера, но не повезло. Я не получаю никаких ошибок, просто сохраняя объект Product и игнорируя два других объекта. –

+0

@JurgenFeuchter Вы пытались изменить структуру массива так, чтобы она соответствовала той, что была в этом ответе? – dragmosh

+0

Я только что понял, что псевдонимы, используемые в отношениях, не соответствуют тем, которые используются в '$ singleProduct'. См. Мое редактирование. –

Смежные вопросы