php - 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;
}
解决方案
推荐阅读
- shell - 在“执行外壳”构建步骤中调用的外壳脚本失败,但 Jenkins 构建被标记为通过
- c# - 如何访问 ASP.Net Core 控制器方法内外的 IApplicationBuilder 和 IWebHost 实例?
- d3.js - d3.js v5 混合基线 tspan
- mysql - 从mysql日期获取月份和年份作为字符串
- javascript - 如何添加在 AJAX 响应中收到的脚本标签
- django - 如何获得所有芹菜任务?
- php - PHP连接到MySQL表的问题
- github - 如何更改分支的基本分支
- c++ - SDL 窗口 - 渲染精灵 C++
- javascript - 管道中的流错误处理 - 停止前一个流