我正在尝试从productfeed导入产品并将其保存到我的数据库中。现在我很难用cakePHP完成这项工作。填写“产品”表非常容易,但很难填写“条款”并加入表“products_terms”。我正在使用交易($ dataSource = $ this-> Supplier-> Product-> getDataSource();)来导入产品,以确保不会将不完整的数据保存到数据库中。现在因为条款表使用AutoIncrement作为术语id,我很难填充products_terms表,因为只有在我知道了术语的id之后才能这样做。如果该ID尚不存在,我无法知道在products_terms表中填写什么。请参阅下面的ERD
https://www.dropbox.com/s/pfakd7xplsvr7cc/db_erd.PNG
有没有人知道这方面的解决方案?
我发送到数据库的数组
array(
'Product' => array(
'slug' => 'c24b626d6701d3b07e30b233b989ff8811',
'product_name' => 'DAHLIA 5A',
'price_new' => '159.00',
'affiliate_url' => 'http://ad.zanox.com/ppc/?utm_source=zanox&utm_medium=banner&utm_campaign=product]]',
'product_id_supplier' => 'c24b626d670133d3b07e30b2b989ff8811',
'supplier_id' => 'zizigo_tr',
'feedimport_id' => (int) 1
),
'Image' => array(
(int) 0 => array(
'image' => 'zizigo_tr_5290e6d296084.JPG'
)
),
'Term' => array(
'Term' => array(
(int) 0 => array(
'name' => 'Tommy Hilfiger'
)
)
)
)
我的模特:
class Product extends AppModel {
public $name = 'Product';
public $primaryKey = 'product_id';
public $hasMany = array(
'Vote',
'Image'
);
public $hasAndBelongsToMany = array(
'Term' => array(
'className' => 'Term',
'joinTable' => 'products_terms',
'foreignKey' => 'product_id',
'associationForeignKey' => 'term_id',
'unique' => true
)
);
public $belongsTo = array(
'Supplier'
);
}
class Term extends AppModel {
public $name = 'Term';
public $primaryKey = 'term_id';
public $hasAndBelongsToMany = array(
'Product' => array(
'className' => 'Product',
'joinTable' => 'products_terms',
'foreignKey' => 'term_id',
'associationForeignKey' => 'product_id',
'unique' => true
)
);
}
答案 0 :(得分:0)
问题解决了。似乎条款表需要填写,否则cakePHP不知道连接哪个术语。
因此,正确的解决方案是首先检查或术语是否存在,否则在术语表中插入术语,然后使用HABTM插入产品(使用插入数组中术语的ID),因此products_terms表将也被填补。