CakePHP Saving 有很多关联
CakePHP Saving hasMany association
我有一个产品需要将图像路径保存到另一个 table。 product hasMany images
.
这是我的add()
if ($this->request->is('post')) {
// https://book.cakephp.org/3.0/en/orm/saving-data.html#saving-associations
$save = $this->Products->newEntity(
$this->request->getData(),
['associated'=>$associated]
);
$this->log('JUST AFTER PATCH'.$save);
$save->last_modified = Time::now();
$save->created = Time::now();
//sort out assoc.
$path = $this->saveImageGetPath($this->request->data['images']);
$save['images'][] = [
'path' => $path,
'alt_description' => $product->name . Time::now() . 'img',
'position' => $this->request->data['position']
];
$save->isDirty(true);
$this->log('JUST BEFORE SAVE'.$save);
if ($this->Products->save($save, [
'associated'=> ['Shoes', 'Images', 'Products_has_categories']
])) {
这是输出到日志的数组
{
"name":"xlc",
"brands_id":1,
"description":"csvfd",
"type":"s",
"position":"0",
"shoe":{
"heels_id":1,
"closures_id":1,
"materials_upper_id":1,
"materials_lining_id":1,
"materials_sole_id":1
},
"products_has_categories":{
"categories_id":"1"
},
"images":[
{
"path":"\/webroot\/img\/products\/img1503289958.jpg",
"alt_description":"8\/21\/17, 4:32 AMimg",
"position":"0"
}
],
"last_modified":"2017-08-21T04:32:38+00:00",
"created":"2017-08-21T04:32:38+00:00"
}
这是 table Image
协会
$this->hasMany('Images', [
'foreignKey' => 'products_id',
'dependent' => true,
]);
图片上传正常,可以获取路径。它只是没有为此触发 SQL 声明,我很困惑为什么
请注意,hasOne
关联。确实有效,所以我可以保存 assoc。但不是这个 hasMany
你将图像作为一个数组添加到数据中,那是行不通的,ORM 将只保存实体对象,即你必须创建一个 \Cake\Datasource\EntityInterface
的实例(这就是entity creation/patching 进程将自动处理传递的数据。
$save['images'][] = $this->Products->Images->newEntity([
'path' => $path,
'alt_description' => $product->name . Time::now() . 'img',
'position' => $this->request->data['position']
]);
您还需要确保 images
属性 被标记为脏,否则 ORM 将忽略它(这也在实体 creation/patching 中自动完成)过程)。您的 isDirty(true)
调用不会执行任何操作,因为 isDirty()
不是 setter,而是 getter.
$save->setDirty('images', true); // use dirty('images', true) in CakePHP < 3.4
另外,不要记录 JSON 表示,最好使用 debug()
,或者至少 var_export()
,以便保留实体提供的调试信息。
另见
我有一个产品需要将图像路径保存到另一个 table。 product hasMany images
.
这是我的add()
if ($this->request->is('post')) {
// https://book.cakephp.org/3.0/en/orm/saving-data.html#saving-associations
$save = $this->Products->newEntity(
$this->request->getData(),
['associated'=>$associated]
);
$this->log('JUST AFTER PATCH'.$save);
$save->last_modified = Time::now();
$save->created = Time::now();
//sort out assoc.
$path = $this->saveImageGetPath($this->request->data['images']);
$save['images'][] = [
'path' => $path,
'alt_description' => $product->name . Time::now() . 'img',
'position' => $this->request->data['position']
];
$save->isDirty(true);
$this->log('JUST BEFORE SAVE'.$save);
if ($this->Products->save($save, [
'associated'=> ['Shoes', 'Images', 'Products_has_categories']
])) {
这是输出到日志的数组
{
"name":"xlc",
"brands_id":1,
"description":"csvfd",
"type":"s",
"position":"0",
"shoe":{
"heels_id":1,
"closures_id":1,
"materials_upper_id":1,
"materials_lining_id":1,
"materials_sole_id":1
},
"products_has_categories":{
"categories_id":"1"
},
"images":[
{
"path":"\/webroot\/img\/products\/img1503289958.jpg",
"alt_description":"8\/21\/17, 4:32 AMimg",
"position":"0"
}
],
"last_modified":"2017-08-21T04:32:38+00:00",
"created":"2017-08-21T04:32:38+00:00"
}
这是 table Image
协会
$this->hasMany('Images', [
'foreignKey' => 'products_id',
'dependent' => true,
]);
图片上传正常,可以获取路径。它只是没有为此触发 SQL 声明,我很困惑为什么
请注意,hasOne
关联。确实有效,所以我可以保存 assoc。但不是这个 hasMany
你将图像作为一个数组添加到数据中,那是行不通的,ORM 将只保存实体对象,即你必须创建一个 \Cake\Datasource\EntityInterface
的实例(这就是entity creation/patching 进程将自动处理传递的数据。
$save['images'][] = $this->Products->Images->newEntity([
'path' => $path,
'alt_description' => $product->name . Time::now() . 'img',
'position' => $this->request->data['position']
]);
您还需要确保 images
属性 被标记为脏,否则 ORM 将忽略它(这也在实体 creation/patching 中自动完成)过程)。您的 isDirty(true)
调用不会执行任何操作,因为 isDirty()
不是 setter,而是 getter.
$save->setDirty('images', true); // use dirty('images', true) in CakePHP < 3.4
另外,不要记录 JSON 表示,最好使用 debug()
,或者至少 var_export()
,以便保留实体提供的调试信息。
另见