首页 > 解决方案 > Symfony 5 表单句柄请求正在执行相同的查询两次

问题描述

所以我正在启动一个新的 Symfony 5 应用程序,并且在编辑控制器中遇到了一些问题,恕我直言,symfony 正在执行一个愚蠢的查询。

所以,下面是我的控制器的代码,在哪里执行重复查询和查询。如果有人可以帮助我,将非常感激,如果需要更多代码,请告诉我。

重复查询,只是在提交请求时发生,没有得到。所以只是在 POST 方法中。

编辑:所以...我发现了一些导致 formtype 的东西'by_reference'=> false,但我想使用它。所以我可以用一种丑陋的方式解决它,这样做:

$product = $this->productRepository->find($id);
$product->getImages()->initialize();

看起来,如果我预加载实体,它可以正常工作,可能是因为通过引用而不是从实体缓存中获取的东西,就像文档中的示例:

来自 symfony 文档:https ://symfony.com/doc/current/reference/forms/types/collection.html

// If you set by_reference to false, submitting looks like this:
$article->setTitle('...');
$author = clone $article->getAuthor();
$author->setName('...');
$author->setEmail('...');
$article->setAuthor($author);
class ProductController extends AbstractController
{
    public function editProduct(Request $request, int $id)
    {
        // Here it executes the query for getting the product, its ok
        $product = $this->productRepository->find($id);

        // Here it executes the query of the images, for handling the forms
        $form = $this->createForm(ProductType::class, $product);

        // Here is the problem, it executes the same query AGAIN!
        $form->handleRequest($request);
    }
}

所以,我不知道为什么当我调用它时$form->handleRequest(),它会执行一个已经运行的查询,下面是一些有用的代码,也许我搞砸了。

Query runned twice:
SELECT t0.id AS id_1, t0.image AS image_2, t0.product_id AS product_id_3 FROM product_image t0 WHERE t0.product_id = ?
class ProductType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
            $builder
                ->add('images', CollectionType::class, [
                    'label'             => false,
                    'entry_type'        => ImageType::class,
                    'entry_options' => [
                        'label' => false,
                    ],
                    'prototype'         => true,
                    'allow_add'         => true,
                    'allow_delete'      => true,
                    'by_reference'      => false,
                    'required'          => false,
                    'attr' => ['class' => 'row text-center text-lg-left'],
                ]);

    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Product::class,
        ]);
    }

class: App\Entity\Catalog\Product
/**
 * @ORM\Entity(repositoryClass=ProductRepository::class)
 */
class Product
{
    /**
     * @ORM\OneToMany(targetEntity="ProductImage", mappedBy="product", cascade={"persist"}, orphanRemoval=true)
     */
    private $images;

    /**
     * @return ProductImage[]|ArrayCollection
     */
    public function getImages()
    {
        return $this->images;
    }

    /**
     * @param ProductImage $productImage
     * @return Product
     */
    public function addImage(ProductImage $productImage): self
    {
        $productImage->setProduct($this);
        $this->images->add($productImage);

        return $this;
    }

    /**
     * @param ProductImage $productImage
     * @return $this
     */
    public function removeImage(ProductImage $productImage): self
    {
        $productImage->setProduct(null);
        $this->images->removeElement($productImage);
        return $this;
    }

}
class: App\Entity\Catalog\ProductImage
/**
 * @ORM\Entity(repositoryClass=ProductImageRepository::class)
 */
class ProductImage
{
    /**
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="images")
     * @ORM\JoinColumn(name="product_id", onDelete="CASCADE")
     */
    private $product;
}

标签: phpsymfonydoctrine

解决方案


推荐阅读