【问题标题】:symfony2 doctrine2 not flushing new entitysymfony2教义2不刷新新实体
【发布时间】:2015-02-01 02:18:31
【问题描述】:

我有一个链接到配方对象的表单。 这个食谱对象有 recipeCategories。

在修改配方时,除了添加新类别之外,更新任何字段都非常有用。

我的表格:

$builder
    ->add('recipeCategories', 'entity',array(
            'multiple' => true,
            'class' => 'AppBundle:FoodAnalytics\RecipeCategory',
            'label' => 'Catégories',
            'required' => false,
            'attr'=>array(
                'data-toggle'=>"tooltip",
                'data-placement'=>"top",
                'title'=>"Indiquez les catégories dans lesquelles enregistrer la recette pour un recherche future plus facile",
            )))

这是我得到的:

    exit(var_dump($request->request));

请求确实有 recipeCategories

    $recipeForm->handleRequest($request);
    exit(var_dump($recipeForm->getData()->getRecipeCategories()->toArray()));

表单确实有 recipeCategories

    $formManager->getEntityManager()->persist($recipe);
    exit(var_dump($recipe->getRecipeCategories()->toArray()));

食谱确实有分类

    $formManager->getEntityManager()->flush($recipe);
    exit(var_dump($formManager->getEntityManager()->getUnitOfWork()->getScheduledEntityInsertions()));

不管有没有冲洗,我在这里得到一个空值

我正在运行 symfony ~2.6.0 和教义 ~1.3,并将 composer prefer-stable 设置为 true。

知道我可以做些什么来让它发挥作用吗?其他字段正确保留。 我的实体通过以下方式链接:

/**
 * @ORM\ManyToMany(targetEntity="\AppBundle\Entity\FoodAnalytics\Recipe", inversedBy="recipeCategories")
 * @ORM\JoinTable(
 *     name="RecipeCategoryHasRecipe",
 *     joinColumns={@ORM\JoinColumn(name="recipeCategoryId", referencedColumnName="id", nullable=false)},
 *     inverseJoinColumns={@ORM\JoinColumn(name="recipeId", referencedColumnName="id", nullable=false)}
 * )
 */
private $recipes;

【问题讨论】:

    标签: php symfony doctrine-orm entity flush


    【解决方案1】:

    可能您需要在关系定义中设置cascade 参数:

    /**
     * @ORM\ManyToMany(targetEntity="\AppBundle\Entity\FoodAnalytics\Recipe",     inversedBy="recipeCategories", cascade={"persist"})
     * @ORM\JoinTable(
     *     name="RecipeCategoryHasRecipe",
     *     joinColumns={@ORM\JoinColumn(name="recipeCategoryId", referencedColumnName="id", nullable=false)},
     *     inverseJoinColumns={@ORM\JoinColumn(name="recipeId", referencedColumnName="id", nullable=false)}
     * )
     */
    private $recipes;
    

    【讨论】:

    • 类别实际上已经存在,我使用实体字段类型来允许它们选择。所以不幸的是,级联并没有改变它,连接表中没有新记录。知道如何进一步调试吗?
    • @SébastienVassaux,您可以尝试在表单字段中手动设置property_path,但这只是猜测。
    • 'property_path' => 'recipeCategories' 不起作用,我需要将值更改为其他值吗?
    • @SébastienVassaux,它应该是您在实体中的属性的名称,在这种情况下:recipes
    • 好吧,它给我一个食谱错误,在食谱实体中无权访问它。确实,表单类是配方,字段实体是 RecipeCategories...所以我想这还不是解决方案。
    【解决方案2】:

    我想通了!

    我认为在表单中定义 by_reference => false 仅适用于集合,但这里也是如此。我必须添加以下代码:

    /**
     * Add recipeCategory
     *
     * @param RecipeCategory $recipeCategory
     *
     * @return Recipe
     */
    public function addRecipeCategory(RecipeCategory $recipeCategory)
    {
        $recipeCategory->addRecipe($this); //makes the difference
        $this->recipeCategories[] = $recipeCategory;
    
        return $this;
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多