gpt4 book ai didi

交响乐。提交表单后,其 CollectionType 字段的 ArrayCollection 中不包含单个元素

转载 作者:行者123 更新时间:2023-12-01 04:44:19 24 4
gpt4 key购买 nike

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

1) App\Entity\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) App\Entity\Platform\Image

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';
}
}

这是我的 Controller 中的代码,其中创建了包含 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 共享项目。

最佳答案

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

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

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

关于交响乐。提交表单后,其 CollectionType 字段的 ArrayCollection 中不包含单个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48139080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com