4

我的模型包含两个相关的类——RealEstate 和 Image,而 RealEstate 的一个实例可以是很多 Image 的实例。由于 Image 类也可以与其他类关联使用,因此我选择了关系“一对多,单向连接表”。这确保了任何图像都不需要知道它在哪里使用。反过来,RealProperty 类提供了 $images 属性、getImages()、addImage(Image $image) 和 removeImage(Image $image) 方法,并且构造函数中的 $images 由空的 ArrayCollection 定义。所以,我有以下模型类。

1) 应用\实体\RealProperty\RealProperty

namespace App\Entity\RealProperty;

use App\Entity\Platform\Image;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\RealProperty\RealPropertyRepository")
 * @ORM\Table(name="real_property")
 */
class RealProperty
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * Many real properties have many images
     * @ORM\ManyToMany(targetEntity="App\Entity\Platform\Image", cascade={"all"})
     * @ORM\JoinTable(name="real_property_images",
     *      joinColumns={@ORM\JoinColumn(name="real_property_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="image_id", referencedColumnName="id", unique=true)}
     *      )
     */
    private $images;

    /**
     * RealProperty constructor
     */
    public function __construct()
    {
        $this->images = new ArrayCollection();
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return mixed
     */
    public function getImages()
    {
        return $this->images;
    }

    /**
     * @param Image $image
     */
    public function addImage(Image $image)
    {
        if (!$this->images->contains($image)) {
            $this->images->add($image);
        }
    }

    /**
     * @param Image $image
     */
    public function removeImage(Image $image)
    {
        $this->images->removeElement($image);
    }
}

2) 应用\实体\平台\图像

namespace App\Entity\Platform;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity(repositoryClass="App\Repository\Platform\ImageRepository")
 * @Vich\Uploadable
 */
class Image
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * NOTE: This is not a mapped field of entity metadata, just a simple property.
     *
     * @Vich\UploadableField(mapping="image", fileNameProperty="imageName", size="imageSize")
     *
     * @var File
     */
    private $imageFile;

    /**
     * @ORM\Column(type="string", length=255, nullable=false)
     *
     * @var string
     */
    private $imageName;

    /**
     * @ORM\Column(type="integer")
     *
     * @var integer
     */
    private $imageSize;

    /**
     * @ORM\Column(type="datetime", nullable=false)
     * @var \DateTime
     */
    private $dateOfCreation;

    /**
     * @ORM\Column(type="datetime", nullable=false)
     * @var \DateTime
     */
    private $dateOfChange;

    /**
     * Image constructor
     */
    public function __construct()
    {
        $currentDate = new \DateTime('NOW');
        $this->dateOfCreation = $currentDate;
        $this->dateOfChange = $currentDate;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return File
     */
    public function getImageFile(): ?File
    {
        return $this->imageFile;
    }

    /**
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
     * of 'UploadedFile' is injected into this setter to trigger the  update. If this
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
     * must be able to accept an instance of 'File' as the bundle will inject one here
     * during Doctrine hydration.
     *
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
     */
    public function setImageFile(?File $image = null): void
    {
        $this->imageFile = $image;

        if (null !== $image) {
            // It is required that at least one field changes if you are using doctrine
            // otherwise the event listeners won't be called and the file is lost
            $this->dateOfChange = new \DateTimeImmutable();
        }
    }

    /**
     * @return string
     */
    public function getImageName(): ?string
    {
        return $this->imageName;
    }

    /**
     * @param string $imageName
     */
    public function setImageName(?string $imageName)
    {
        $this->imageName = $imageName;
    }

    /**
     * @return int
     */
    public function getImageSize(): ?int
    {
        return $this->imageSize;
    }

    /**
     * @param int $imageSize
     */
    public function setImageSize(?int $imageSize)
    {
        $this->imageSize = $imageSize;
    }

    /**
     * @return \DateTime
     */
    public function getDateOfCreation(): ?\DateTime
    {
        return $this->dateOfCreation;
    }

    /**
     * @param \DateTime $dateOfCreation
     */
    public function setDateOfCreation(?\DateTime $dateOfCreation)
    {
        $this->dateOfCreation = $dateOfCreation;
    }

    /**
     * @return \DateTime
     */
    public function getDateOfChange(): ?\DateTime
    {
        return $this->dateOfChange;
    }

    /**
     * @param \DateTime $dateOfChange
     */
    public function setDateOfChange(?\DateTime $dateOfChange)
    {
        $this->dateOfChange = $dateOfChange;
    }
}

对于每个类,我创建了适当的表单类型。

1) App\Form\RealProperty\RealPropertyType

namespace App\Form\RealProperty;

use App\Entity\RealProperty\RealProperty;
use App\Form\Platform\ImageType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class RealPropertyType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('images', CollectionType::class, array(
                'entry_type' => ImageType::class,
                'label' => false,
                'allow_add'  => true,
                'allow_delete' => true,
                'prototype' => true,
                'by_reference' => false
            ))
            ->add('submit', SubmitType::class, [
                'label' => 'Сохранить',
                'attr' => [
                    'class' => 'btn btn-sm btn-primary col-6 mx-auto',
                    'style' => 'display: block;'
                ]
            ])
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'App\Entity\RealProperty\RealProperty'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'real_property_real_property';
    }
}

2) App\Form\Platform\ImageType

<?php

namespace App\Form\Platform;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichImageType;

class ImageType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('imageFile', VichImageType::class, array(
                'label' => false,
                'required' => true
            ))
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'App\Entity\Platform\Image'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'platform_image';
    }
}

这是我的控制器中的代码,其中创建了包含 CollectionType 的表单。

<?php

namespace App\Controller\RealProperty;

use App\Entity\RealProperty\RealProperty;
use App\Form\RealProperty\RealPropertyType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Class RealPropertyController
 *
 * @Route("real_property")
 * @package App\Controller\RealProperty
 */
class RealPropertyController extends Controller
{
    /**
     * Creates a new real property entity
     *
     * @Route("/new", name="real_property_new")
     * @Method({"GET", "POST"})
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
     */
    public function newAction(Request $request) {
        $realProperty = new RealProperty();
        $form = $this->createForm(RealPropertyType::class, $realProperty);
        $form->handleRequest($request);

//        dump($form->getData());
//        dump($realProperty);
//        dump($realProperty->getImages());
//        dump($request->get('images'));

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($realProperty);
            $em->flush();

            return $this->redirectToRoute('real_property_index');
        }

        return $this->render('RealProperty/RealProperty/new.html.twig', [
            'realProperty'  => $realProperty,
            'form'          => $form->createView(),
        ]);
    }
}

但是,必须包含 Image 实例的 ArrayCollection 始终为空,但在客户端,CollectionType 的所有子字段都包含它们的图像。

我们可以假设我错误地配置了 Vich/UploaderBundle,没有在服务器目录中保存图像的权限,数据库模式描述不正确......但是 - 不!一切都是正确的。特别是为此,我创建了一个单独的 ImageController,它在 newAction() 中创建了 ImageType 表单,并且所有图像都安全地存储在数据库中。所以问题出在 ArrayCollection 级别或“一对多,单向与连接表”关系级别的某个地方。我认同。

请帮助,找出这个陷阱。我会很感激。如有必要 - 我可以通过 git 共享项目。

4

2 回答 2

1

提交表单时可以打开 Symfony Profiler 吗?

“表单”选项卡将为您提供有关如何处理数据的每个字段的详细信息,包括模型、规范化和视图格式。

这可能会帮助您检查表单组件之前和曾经处理过的字段的类型。

于 2018-01-18T12:23:11.220 回答
0

我认为你应该使用VichImageType Field. 在此链接中,您可以看到正确的方法。为什么要Id手动设置App\Entity\Platform\Image?这是 VichImageType 代码:

use Vich\UploaderBundle\Form\Type\VichImageType;

class Form extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        // ...

        $builder->add('imageFile', VichImageType::class, [
            'required' => false,
            'allow_delete' => true,
            'download_label' => '...',
            'download_uri' => true,
            'image_uri' => true,
            'imagine_pattern' => '...',
        ]);
    }
}
于 2018-01-12T11:44:32.853 回答