【问题标题】:Symfony 2 collection with image - editingSymfony 2 合集与图像 - 编辑
【发布时间】:2014-03-25 19:47:17
【问题描述】:

我的收藏表单类型没有什么问题。

我有一对多的关系(产品和产品图片)

我认为问题出在我的 productImages 实体中(如果可以解决的话)

我的产品图片:

class produktImage
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

/**
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;

/**
 * @Assert\File(maxSize="6000000")
 */
public $file;

/**
* @ORM\ManyToOne(targetEntity="product", inversedBy="tags")
*/
protected $product;

还有最重要的方法:

 public function upload()
    {        
        $this->name = trim(date("dmyGis").rand(1,99999999).'_'.$this->file->getClientOriginalName());
        $this->file->move($this->getUploadRootDir(), $this->name);

        $move = new resizeImg(1280, 3000, $this->name, $this->getUploadRootDir(), null);
        $move->setThumbial(550, 2000, $this->getUploadRootDir().'../small/', null);
        $move->setThumbial(200, 2000, $this->getUploadRootDir().'../thumb/', null);

        $this->file = null;
    }

Form productImage 只有文件字段。

我的产品实体(最重要的方法)

    public function addPhotosum($photos)
    {
        $photos->setProdukt($this);
        $photos->upload($this);
        $this->photos->add($photos);
        return $this;
    }

    public function removePhotosum($photos)
    {
        $this->photos->removeElement($photos);
        $photos->removeImg();
    }

好的,但我的问题在哪里。 当我尝试添加或删除文件时,一切正常。 如果我尝试编辑文件没有任何反应。名称和文件不变。

我认为这是因为 Product 看不到名称的更改(名称仅存储在 db 中),但我不知道如何告诉他“当文件不同时更改名称和文件”。 我可以使用 preUpdate 或其他东西吗?
有人遇到过类似问题吗?

【问题讨论】:

    标签: forms symfony collections


    【解决方案1】:

    只需在控制器的编辑操作中调用 upload() 方法..

    例子:

    public function editAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();
    
        $entity = $em->getRepository('YourBundle:YourEntity')->find($id);
    
        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Your entity.');
        }
    
        $editForm = $this->createEditForm($entity);
        $editForm->handleRequest($request);
    
        if ($editForm->isValid()) {
            $entity->upload()
            $entity->persist($entity);
            $em->flush();
    
            return $this->redirect($this->generateUrl('your_destination_route', array('id' => $id)));
        }
    
        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
        );
    }
    

    我希望它对你有用..

    【讨论】:

      猜你喜欢
      • 2014-12-25
      • 1970-01-01
      • 2018-01-14
      • 1970-01-01
      • 2014-10-01
      • 1970-01-01
      • 2014-08-21
      • 2012-10-30
      • 1970-01-01
      相关资源
      最近更新 更多