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

时间:2015-02-16 14:06:42

标签: php symfony doctrine-orm tree doctrine-extensions

我尝试在symfony2中为一个教义嵌套关系添加一个表单中的子元素。 但这种关系并没有得到保存。

我使用Doctrine Extensions,所有其他行为都按预期工作。

我的自我实体:

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;
}




}

我的表格:

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
            ))
;

}

我的控制器:

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(),
    ));
}
你能帮帮我吗?

0 个答案:

没有答案