douwei1944 2015-02-16 14:06
浏览 34

无法在symfony2中以嵌套树形式保存子项

I try to add childrens in a form for a doctrine nested relation in symfony2. But the relations are not saved.

I use the Doctrine Extensions and all other behaviours works as expected.

My self related entity:

class Article
{
/**
 * @Gedmo\TreeLeft
 * @ORM\Column(name="lft", type="integer")
 */
private $lft;

/**
 * @Gedmo\TreeLevel
 * @ORM\Column(name="lvl", type="integer")
 */
private $lvl;

/**
 * @Gedmo\TreeRight
 * @ORM\Column(name="rgt", type="integer")
 */
private $rgt;

/**
 * @Gedmo\TreeRoot
 * @ORM\Column(name="root", type="integer", nullable=true)
 */
private $root;

/**
 * @Gedmo\TreeParent
 * @ORM\ManyToOne(targetEntity="Article", inversedBy="children", cascade={"persist"})
 */
private $parent;

/**
 * @ORM\OneToMany(targetEntity="Article", mappedBy="parent", cascade={"persist"})
 */
private $children;

 /**
 * Set lft
 *
 * @param integer $lft
 * @return Article
 */
public function setLft($lft)
{
    $this->lft = $lft;

    return $this;
}

/**
 * Get lft
 *
 * @return integer 
 */
public function getLft()
{
    return $this->lft;
}

/**
 * Set lvl
 *
 * @param integer $lvl
 * @return Article
 */
public function setLvl($lvl)
{
    $this->lvl = $lvl;

    return $this;
}

/**
 * Get lvl
 *
 * @return integer 
 */
public function getLvl()
{
    return $this->lvl;
}

/**
 * Set rgt
 *
 * @param integer $rgt
 * @return Article
 */
public function setRgt($rgt)
{
    $this->rgt = $rgt;

    return $this;
}

/**
 * Get rgt
 *
 * @return integer 
 */
public function getRgt()
{
    return $this->rgt;
}

/**
 * Set root
 *
 * @param integer $root
 * @return Article
 */
public function setRoot($root)
{
    $this->root = $root;

    return $this;
}

/**
 * Get root
 *
 * @return integer 
 */
public function getRoot()
{
    return $this->root;
}

/**
 * Set parent
 *
 * @param \CmsBundle\Entity\Article $parent
 * @return Article
 */
public function setParent(\CmsBundle\Entity\Article $parent = null)
{
    $this->parent = $parent;

    return $this;
}

/**
 * Get parent
 *
 * @return \CmsBundle\Entity\Article 
 */
public function getParent()
{
    return $this->parent;
}

/**
 * Add children
 *
 * @param \CmsBundle\Entity\Article $children
 * @return Article
 */
public function addChild(\CmsBundle\Entity\Article $children)
{
    $this->children[] = $children;
    $children->setParent($this);
    return $this;
}

/**
 * Remove children
 *
 * @param \CmsBundle\Entity\Article $children
 */
public function removeChild(\CmsBundle\Entity\Article $children)
{
    $this->children->removeElement($children);
    $children->setParent(null);
    return $this;
}

/**
 * Get children
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getChildren()
{
    return $this->children;
}




}

My form:

public function buildForm(FormBuilderInterface $builder, array $options)
{

$entity = $builder->getData();

$builder
    ->add('title')
    ->add('children', 'entity', array(
                'class' => 'MyBundle:Article',
                'property' => 'title',
                'query_builder' => function(\CmsBundle\Entity\ArticleRepository $er ) use (  $entity ) {
                                return $er->findChildableEntitiesQb($entity->getId());
                        },
                'multiple' => true,
                'expanded'=>true
            ))
;

}

My controller:

public function updateAction(Request $request, $id)

{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('CmsBundle:Article')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Article entity.');
    }

    $deleteForm = $this->createDeleteForm($id);
    $editForm = $this->createEditForm($entity);
    $editForm->handleRequest($request);


    if ($editForm->isValid()) {
        $em->persist($entity);

        $em->flush();

        return $this->redirect($this->generateUrl('article_edit', array('id' => $id)));
    }

    return $this->render('CmsBundle:Article:edit.html.twig', array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}

Could you please help me ?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大