CakePHP cake saveall不保存关系模型
我正在尝试创建一个对象,其中包含保存CakePHP cake saveall不保存关系模型,cakephp,orm,model,relation,cakephp-2.x,Cakephp,Orm,Model,Relation,Cakephp 2.x,我正在尝试创建一个对象,其中包含保存产品、其n变体及其n图像所需的所有字段。但是由于某种原因,saveAll()无法工作 我试着用它的变体和图像保存一系列产品,但没有成功。它也只保存了产品。我确实在模型中建立了关系 Product.php: public $hasMany = array( 'product_variant' => array( 'className' => 'ProductVariant', 'foreignKey' =&g
产品
、其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' => ''
)
);
//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' => ''
)
);
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Product' => array(
'className' => 'Product',
'foreignKey' => 'product_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
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
)
);
ProductVariant.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' => ''
)
);
//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' => ''
)
);
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Product' => array(
'className' => 'Product',
'foreignKey' => 'product_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
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
)
);
和ProductImage.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' => ''
)
);
//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' => ''
)
);
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Product' => array(
'className' => 'Product',
'foreignKey' => 'product_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
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
)
);
以下是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>";
}
我回到原点D:它只保存产品图片。我已经检查过了,模型现在已经修正了。但它不会保存变体或图像
有什么想法吗?o、 o您的数据结构不正确。为了能够保存
有许多
数据,您必须将以下内容提供给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' => ''
)
);
//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' => ''
)
);
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Product' => array(
'className' => 'Product',
'foreignKey' => 'product_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
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
)
);
看
- 在Cookbook 2.x中保存数据
$singleProduct
中使用的别名不匹配。查看我的编辑。您应该能够通过在saveAll()
或saveMany()
上设置'deep'=>true
来保存数组。但是,根据文档,saveAll()
也应该可以工作,因为它是saveMany()
和saveAssociated()
的包装。