【问题标题】:CakePHP 3: How to update foreignKey?CakePHP 3:如何更新外键?
【发布时间】:2016-05-20 20:57:53
【问题描述】:

我使用BlueImp jQuery插件在以下场景中上传多张图片:

  1. 用户打开添加或编辑相册页面,选择图像并通过 ajax 方法处理上传。这项工作很好,图像数据(名称,大小,模型,字段,..)存储在相关的图像表中,没有外键。

  2. Ajax 返回最近上传图片的 id,js 为该文件创建(标题、位置)输入。

  3. 用户填写其他表单字段,然后点击提交。

问题,如果用户创建新相册或编辑/更新存在不包含图像的相册,应用程序不会更新新图像的外键。但是如果相册包含图片,并且用户在相册中添加新的,在保存后 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?

【问题讨论】:

    标签: cakephp cakephp-3.2


    【解决方案1】:

    我不确定这是不是正确的方法,但它对我有用。

    相册表

    public function afterSave(Event $event, Entity $entity, ArrayObject $options)
    {
       if (isset($entity)) {
            $images = TableRegistry::get('Images');
            $images->query()
                ->update()
                ->set(['foreign_key' => $entity->id])
                ->where([
                    'foreign_key IS NULL',
                    'model' => 'Albums'
                    ])
                ->execute();// conditions
               }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 2016-01-20
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      相关资源
      最近更新 更多