我使用BlueImp jQuery插件在后续场景中上传多张图片:
用户打开添加或编辑相册页面,选择图像并通过ajax方法上传procces。这项工作很好,图像数据(名称,大小,型号,字段,..)存储在没有外键的相关图像表中。
Ajax返回最近上传图片的id,以及js为该文件创建(标题,位置)输入。
用户填写其他表单字段,然后单击“提交”。
问题是,如果用户创建新相册或编辑/更新存在不包含图像,则应用程序不会更新新图像的外键。但是如果专辑包含图像,并且用户将新的一个添加到相册,则在保存post cakephp之后将外键添加/更新到新图像记录。
调试输出:
// first time add images, does not update FK
[
'name' => 'A4',
'images' => [
(int) 116 => [
'caption' => '',
'position' => '1',
'id' => '116'
]
],
'id' => '4',
'user_id' => '1',
'active' => '1'
]
// album has one image, we add new one, CakePHP Update FK for new images
[
'name' => 'A4',
'images' => [
(int) 117 => [
'caption' => '',
'position' => '1',
'id' => '117'
],
(int) 118 => [
'caption' => '',
'position' => '2',
'id' => '118'
]
],
'id' => '4',
'user_id' => '1',
'active' => '1'
]
AlbumsController添加方法:
public function add()
{
$album = $this->Albums->newEntity();
if ($this->request->is('post')) {
$album = $this->Albums->patchEntity($album, $this->request->data,['associated' => ['Images']]);
if ($this->Albums->save($album)) {
$this->Flash->success(__('The album has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The album could not be saved. Please, try again.'));
}
}
$users = $this->Albums->Users->find('list', ['limit' => 200]);
$this->set(compact('album', 'users'));
$this->set('_serialize', ['album']);
}
专辑表:
$this->hasMany('Images', [
'foreignKey' => 'foreign_key',
'conditions' => [
'Images.model' => 'Albums',
'Images.field' => 'upload'
],
'sort' => ['Images.position' => 'ASC']
]);
图片表:
$this->belongsTo('Albums', [
'foreignKey' => 'foreign_key',
'joinType' => 'INNER'
]);
我在cakephp 2应用程序中使用相同的方法,并且没有问题。
视频:http://screencast-o-matic.com/watch/cDhYYOinCX
问:如何更新foreignKey?
答案 0 :(得分:0)
我不确定这是否正确,但这对我有用。
AlbumsTable
dependencies {
compile 'com.ogaclejapan.arclayout:library:1.1.0@aar'
}