保存后,我的更新功能不会将值设置为null

问题描述 投票:0回答:1

我收到了帮助,以解决如何删除使用Cakephp上传包上传的文件。但是,我如何更新photodir字段的值似乎存在问题。通过使用unlink,我能够完美地删除文件,但是当我尝试将值设置为null时似乎存在问题。我做了一个测试它的功能:

public function deletePhoto2($id)
{
    // $this->request->allowMethod(['post']);
    if ($this->request->is(['patch', 'post', 'put'])) {

        $brigada = $this->Brigadas
                    ->findById($id)
                    ->firstOrFail();

        $brigada->dir = null;
        $brigada->photo = null;

        if ($this->Brigadas->save($brigada)) {
            $this->Flash->success(__('Your team data has been saved.'));
            return $this->redirect(['action' => 'edit', $brigada->id]);
        }

        $this->set('brigada', $brigada);
    }
}

在保存之前,我发现$brigada->photo$brigada->dir的值为null,但值不保存。我有几种想要探索的可能性,但我对PHP的了解是一个障碍:

  1. 我可能做错了更新。 Link
  2. 我可能需要使用deleteCallback来记录here,但我不知道该怎么做。我认为它会与$this->Brigadas->deleteCallback()或类似的东西,但我想先了解一个例子,这就是我要问的原因。我发现在网络上的任何一个例子中都没有使用这些回调,and the documentation on events is still a bit esoteric for me.

以下是如何设置BrigadasTable.php来上传文件:

    // http://josediazgonzalez.com/2015/12/05/uploading-files-and-images/
    $this->addBehavior('Josegonzalez/Upload.Upload', [
        'photo' => [
            'fields' => [
                'dir' => 'dir',
                'size' => 'photo_size', // defaults to `size`
                'type' => 'photo_type', // defaults to `type`
            ],

            'nameCallback' => function ($table, $entity, $data, $field, $settings) {
                if ($entity->gvCode){
                    $array = explode(".", $data['name']);
                    return strtolower($entity->gvCode) . '_' . date("Ymd-hisa") . '.jpg';

                } else{
                    $array = explode(".", $data['name']);
                    $newArray = array_pop($array);
                    return strtolower(join('_', $array)) . '_' . date("Ymd-hisa") . '.jpg';
                }
            },

            'transformer' =>  function ($table, $entity, $data, $field, $settings) {

                $extension = pathinfo($data['name'], PATHINFO_EXTENSION);

                // Store the thumbnail in a temporary file
                $tmp = tempnam(sys_get_temp_dir(), 'upload') . '.' . $extension;

                // Use the Imagine library to DO THE THING
                $size = new \Imagine\Image\Box(640, 640);
                $mode = \Imagine\Image\ImageInterface::THUMBNAIL_INSET;
                $imagine = new \Imagine\Gd\Imagine();

                // Save that modified file to our temp file
                $imagine->open($data['tmp_name'])
                    ->thumbnail($size, $mode)
                    ->save($tmp);

                $filenameTmp = explode('.', $data['name']);
                array_pop($filenameTmp);
                $filenameTmp = join('_', $filenameTmp) . '.jpg';
                // return debug($filenameTmp);

                // Now return the original *and* the thumbnail
                return [
                    $data['tmp_name'] => $filenameTmp,
                    $tmp => 'thumbnail-' . $filenameTmp,
                ];
            },

            'deleteCallback' => function ($path, $entity, $field, $settings) {
                // When deleting the entity, both the original and the thumbnail will be removed
                // when keepFilesOnDelete is set to false

                $entity->{$field} = null;

                return [
                    $path . $entity->{$field},
                    $path . 'thumbnail-' . $entity->{$field}

                ];
            },
            'keepFilesOnDelete' => false
        ]
    ]);

谢谢!

php cakephp-3.x
1个回答
1
投票

您可以直接运行更新查询,而不是获取记录,设置值并保存它。

$query = $this->Brigadas->query();
$query->update()
    ->set([
            'dir' => null,
            'photo' => null,
         ])
    ->where(<condition>)
    ->execute();

包括你的任何列是json,

$query = $this->Brigadas->query();
$query->update()
    ->set([
            'dir = null',
            'photo' => null,
         ])
    ->where(<condition>)
    ->execute();
© www.soinside.com 2019 - 2024. All rights reserved.