0

我可以在数据库中添加文件详细信息,但无法更新它。

我可以添加文件详细信息条目,但是当我尝试更新时,只有我正在更新的文件被移动到存储文件夹。我的更新管理器没有显示任何错误,也没有更新数据库中的文件详细信息。

这是我的档案表格

protected function addElements() 
{
    // Add "name" field
    $this->add([           
        'type'  => 'file',
        'name' => 'image',
        'attributes' => [
            'id' => 'image'
        ],
        'options' => [
            'label' => 'ImageFile',
        ],
    ]);
    
   
                    
    // Add the Submit button
    $this->add([
        'type'  => 'submit',
        'name' => 'submit',
        'attributes' => [                
            'value' => 'Add Image File',
            'id' => 'submit',
        ],
    ]);
    
    // Add the CSRF field
    $this->add([
        'type' => 'csrf',
        'name' => 'csrf',
        'options' => [
            'csrf_options' => [
            'timeout' => 600
            ]
        ],
    ]);
}

public function addInputFilter()
{
    $inputFilter = new InputFilter\InputFilter();

    // File Input
    $fileInput = new InputFilter\FileInput('image');
    $fileInput->setRequired(true);
   
    $inputFilter->add($fileInput);

    $this->setInputFilter($inputFilter);
}

}

这是更新图像管理器

public function updateImage($name, $size)
{
$images = new Images();
$images->setName($name);
$images->setSize($size);                

// Apply changes to database.
$this->entityManager->flush();

}

这是我的控制器

public function editAction()
{
    
    $id = (int)$this->params()->fromRoute('id', -1);
    if ($id<1) {
        $this->getResponse()->setStatusCode(404);
        return;
    }
    
    $image = $this->entityManager->getRepository(Images::class)
            ->find($id);
    
    if ($image == null) {
        $this->getResponse()->setStatusCode(404);
        return;
    }
    // Create form
    $form = new ImageUploadForm('update', $this->entityManager);
   $request = $this->getRequest();
   if ($this->getRequest()->isPost()) {
        
        $data = array_merge_recursive(
            $request->getPost()->toArray(),
            $request->getFiles()->toArray()
        );            
        
        $form->setData($data);
        
        
        if($form->isValid()) {
            $data = $form->getData();
            $imgtmp = $data["image"]["tmp_name"];
            
            $name = $data["image"]["name"];
            $size = $data["image"]["size"];
            $filepath = $this->_dir.$name;
            move_uploaded_file($imgtmp, $filepath);
            
            $this->achimotaImagesManager->updateImage($name, $size);
            
            var_dump($name, $size);
            return $this->redirect()->toRoute('images', ['action'=>'index']);
        }
    }
    
    return new ViewModel([
        'form' => $form,
    ]);
}
4

1 回答 1

0

不要创建新对象

如果您更新一个图像实体(如果它是一个图像,请考虑将其命名为图像),您不应该创建一个新实体。交出你需要更新的 $image:

public function updateImage($image, $name, $size){
    $image->setName($name);
    $image->setSize($size);
    ...
}

冲洗前坚持

您需要在刷新之前保留实体。

$this->entityManager->persist($image);
$this->entityManager->flush();

更好地组织代码

  1. 不要在控制器中注入实体管理器。而是通过工厂注入服务,该工厂处理您的 Image 实体的所有功能。(图像服务.php)
  2. 也不要将实体管理器注入到您的 ImageService 中。创建一个 ImageMapper 服务,将其注入到您的 ImageService 中。在此 Mapper 中创建所有与 Doctrine 相关的功能。这有这个优势:特定于 Doctrin 的功能仅在您的 Mapper 文件中。如果您需要使用其他解决方案来存储数据,您只需要替换 Mapper 文件,为 Service 提供相同的接口。

控制器

public function editAction()
{
    ...
    $this->serviceImage->update($image,$name,$size);
    ...
}

服务 - ImageService.php

public function update($image,$name,$size)
{
    $image->setName($name);
    $image->setSize($size);
    $this->mapperImage->save($image);
}

映射器 - ImageMapper.php

public function save($image)
{
    $this->managerEntity->persist($image);
    $this->managerEntity->flush();
}

考虑为函数的参数和返回值添加丰富的注释和类型提示。

而且

  1. 不应在您的控制器中创建表单。将该代码也放入您的 ImageService 中。并考虑将表单注入服务。(确保在 getFormElementConfig() 中为工厂定义表单!这是更高级的东西,如果您不使用 phpunit 进行测试,您可能不会费心将表单创建为服务,但它会导致代码库非常有条理。)

  2. var_dump($name, $size) 在您的控制器中没有位置。(如果这是出于调试目的,那没关系,但可以在兼容的 IDE 中使用 XDebug 之类的东西——PHPStorm 是最好的。)

  3. 这行不是那么容易理解: $filepath = $this->_dir.$name; 也许:

    $filePath = _dir 。$名称;

  4. 命名约定:查找 camelCase。

于 2021-05-30T01:59:03.307 回答