Php 我的更新功能没有';t保存后将值设置为null

Php 我的更新功能没有';t保存后将值设置为null,php,cakephp-3.x,Php,Cakephp 3.x,我收到了关于如何使用Cakephp上传包删除上传文件的帮助。但是,如何更新photo和dir字段的值似乎有问题。通过使用unlink,我能够完美地删除文件,但是当我试图将值设置为null时,似乎出现了问题。我做了一个函数来测试它: public function deletePhoto2($id) { // $this->request->allowMethod(['post']); if ($this->request->is(['patch', 'po

我收到了关于如何使用Cakephp上传包删除上传文件的帮助。但是,如何更新
photo
dir
字段的值似乎有问题。通过使用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
的值为空,但值不保存。我想探索几种可能性,但我对PHP的了解是一个障碍:

  • 我可能做得不对
  • 我可能需要使用有文档记录的deleteCallback,但我不知道怎么做。我想应该是用
    $this->Brigadas->deleteCallback()
    或类似的东西,但我想先了解一个例子,这就是为什么我要问这个问题。我发现这些回调在web上的任何示例中都没有用处
  • 下面是如何设置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
            ]
        ]);
    

    谢谢大家!

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

    $query = $this->Brigadas->query();
    $query->update()
        ->set([
                'dir' => null,
                'photo' => null,
             ])
        ->where(<condition>)
        ->execute();
    
    $query=$this->Brigadas->query();
    $query->update()
    ->设置([
    'dir'=>null,
    'photo'=>null,
    ])
    ->where()
    ->执行();
    
    如果您的任何列都是json

    $query = $this->Brigadas->query();
    $query->update()
        ->set([
                'dir = null',
                'photo' => null,
             ])
        ->where(<condition>)
        ->execute();
    
    $query=$this->Brigadas->query();
    $query->update()
    ->设置([
    “dir=null”,
    'photo'=>null,
    ])
    ->where()
    ->执行();
    
    它是否说它保存了,但实际上没有?还是保存失败了?很抱歉我没有早点回复,@GregSchmidt。它确实说它保存了(我将调试函数是否成功返回)。它只是没有更新任何东西。我认为我做的一切都是正确的,因为它在保存之前编辑了呼叫,但在保存之后却没有返回任何内容。我很好奇为什么会这样。我不知道你所说的“它在保存之前编辑了呼叫,但在保存之后却没有返回任何内容”是什么意思,所以我无法对可能出现的错误进行评论。对不起,让我再试一次。当我在
    if($this->Brigadas->save($brigada))
    之前调试时,我将
    dir
    photo
    设置为null,但不是在save函数之后。哦,可能上传行为甚至在这种情况下也设置了它们?在保存此实例之前,请尝试从模型中删除该行为。这很有效!我很高兴,非常感谢!我应该将我的
    取消链接
    放在控制器的另一个函数中,还是有办法不提取记录?您有什么建议?@E.Velis首先运行
    取消链接
    ,然后更新数据库似乎更有意义。