Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony 5表单句柄请求正在执行同一查询两次_Php_Symfony_Doctrine - Fatal编程技术网

Php Symfony 5表单句柄请求正在执行同一查询两次

Php Symfony 5表单句柄请求正在执行同一查询两次,php,symfony,doctrine,Php,Symfony,Doctrine,因此,我启动了一个新的Symfony 5应用程序,在编辑控制器imho中遇到了一些问题,Symfony正在执行一个哑查询 下面是我的控制器的代码,执行重复查询和查询的位置。如果有人能帮助我,将非常感谢,如果需要更多的代码,请让我知道 重复查询只是在提交请求时发生,而不是在获取请求时发生。所以就用POST方法 编辑:所以。。。我发现,在formtype“by_reference”=>false中,某些东西导致了错误,但我想使用它。所以我可以用一种丑陋的方式解决它,做: $product = $th

因此,我启动了一个新的Symfony 5应用程序,在编辑控制器imho中遇到了一些问题,Symfony正在执行一个哑查询

下面是我的控制器的代码,执行重复查询和查询的位置。如果有人能帮助我,将非常感谢,如果需要更多的代码,请让我知道

重复查询只是在提交请求时发生,而不是在获取请求时发生。所以就用POST方法

编辑:所以。。。我发现,在formtype
“by_reference”=>false
中,某些东西导致了错误,但我想使用它。所以我可以用一种丑陋的方式解决它,做:

$product = $this->productRepository->find($id);
$product->getImages()->initialize();
看起来,如果我预加载实体,它工作得很好,可能是因为引用没有从实体缓存中获取某些内容,如文档中的示例:

从symfony文档:

所以,我不知道为什么当我调用
$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 = ?

你绝对确定你的实际控制器代码中没有重定向?看起来很奇怪。离题,但我很确定你不想在你的产品形象中出现onDelete cascade。这意味着您希望在删除图像时删除产品。我肯定,因为我马上退出了代码,所以我知道,这就是调用查询的两个方法,也许我在使用它的其他文件中弄糟了,但是IDK在哪里。关于onDelete级联,您确定不是相反的吗?当产品被删除,图像也被删除?考虑创建一个新的项目,复制粘贴只是两个实体,控制器和窗体连同最小的模板。没有别的了。查看问题是否仍然存在。探查器栏应该为您列出所有查询。如果仍然存在问题,请将新项目签入github并发布链接。另一种选择是继续,将findProductWith Images方法添加到产品存储库中,并将产品和图像加入其中。优点是您只需要一个查询即可获得产品和图像。基本上绕过了延迟加载的东西。不管怎样,你最终可能会想做的事情。仍然无法解释为什么会有两个产品图像查询。我将尝试创建一个新项目并重现错误,只编写基本内容。哦,顺便说一句,这两个问题都会出现,只有当我提交表单时,当获取时,它只运行一次,我会编辑问题以使其更清晰。只要一个问题,你会怎么做?!急件?这是一个好的选择吗?在我看来,这可能不利于缓存,我会更好地控制缓存两个分离查询而不是一个。在未来,产品将有许多其他关系,如类别、属性等。
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);
    }
}
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;
}