1

我有一个属于一个或多个类别的实体,每个实体(Boom)都有许多属性或子元素(Boomelement),在这种情况下它们应该是 7。

当我尝试根据执行顺序保留实体时,我得到不同的结果:

  1. 如果我创建一个空实体,绑定请求,然后坚持:

    • 生成实体
    • 类别被正确分配
    • 子元素被持久化,但它们没有与实体关联,具有外键的列显示为 null
  2. 如果我制作一个没有空实体的表单,则绑定请求并保持:

    • 生成实体
    • 该类别没有得到分配
    • 子元素被持久化并与实体相关联
  3. 如果我使用空实体创建表单并分配 1 个空子元素,则绑定请求然后持久化:

    • 生成实体
    • 类别被分配
    • 只有 1 个子元素与实体关联,其余子元素被持久化但未将列与外键关联显示为 null
  4. 如果我使用空实体创建表单并分配 7 个空子元素,则绑定请求然后持久化:

    • 生成实体
    • 类别被分配
    • 7 个子元素与实体相关联
    • 如果我动态添加更多子元素怎么办,它们将不会被关联

我认为这种行为是不一致和奇怪的,除此之外没有记录。

有没有办法正确地做到这一点?

控制器动作:

public function createAction() {
    $entityEmpty = new Boom();
    //for ($i = 0; $i <= 6; $i++) {
        $element = new Boomelement();
        $entityEmpty->addElement($element);
        //$element->setPosition($i);
    //}

    $form = $this->createForm(new BoomType(),$entityEmpty);
    $request = $this->getRequest();
    $form->bind($request);
    $entity = $form->getData();
    var_dump($entityEmpty);
    var_dump($entity);
    //exit;
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        //return $this->redirect($this->generateUrl('boom_show', array('id' => $entity->getId())));
    }else{
        //var_dump($form->getErrors());
        //var_dump(get_class_methods($form));
    }

    return $this->render('BoomBackBundle:Boom:new.html.php', array(
                'entity' => $entity,
                'form' => $form->createView(),
            ));
}

实体=繁荣;

/**
 * @ORM\Entity(repositoryClass="Boom\Bundle\LibraryBundle\Repository\BoomRepository")
 * @ORM\Table(name="boom")
 * @ORM\HasLifecycleCallbacks
 */
class Boom extends DomainObject
{

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @Gedmo\Slug(fields={"title"})
 * @ORM\Column(type="string", length=140, unique=true)
 */
protected $slug;

/**
 * @ORM\Column(type="string", length=140)
 */
protected $title;

/**
 * @ORM\Column(type="text")
 */
protected $summary;

/**
 * @ORM\Column(type="datetime")
 */
protected $date_created;

/**
 * @ORM\Column(type="datetime", nullable=true)
 * @ORM\Version
 */
protected $date_published;

/**
 * @ORM\Column(type="boolean")
 */
protected $nsfw;

/**
 * @ORM\ManyToOne(targetEntity="Image")
 * @ORM\JoinColumn(name="image_id", referencedColumnName="id", nullable=true)
 **/
protected $image;

/**
 * @ORM\ManyToOne(targetEntity="User", fetch="LAZY")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
 **/
protected $user;

/**
 * @ORM\ManyToOne(targetEntity="Boom", inversedBy="children")
 * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true)
 **/
protected $parent;

/**
 * @ORM\OneToMany(targetEntity="Boom", mappedBy="parent")
 **/
protected $children;


/**
 * @ORM\ManyToMany(targetEntity="Category", inversedBy="categories")
 * @ORM\JoinTable(name="booms_categories",
 *      joinColumns={@ORM\JoinColumn(name="boom_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")}
 *      )
 */
protected $categories;

/**
 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="booms")
 * @ORM\JoinTable(name="booms_tags")
 **/
protected $tags;

/**
 * @ORM\OneToMany(targetEntity="Boomelement", mappedBy="boom", cascade={"persist","remove"}, orphanRemoval=true)
 * @ORM\OrderBy({"position" = "ASC"})
 **/
protected $elements;

public function __construct()
{
    $this->children = new ArrayCollection();
    $this->categories = new ArrayCollection();
    $this->tags = new ArrayCollection();
    $this->elements = new ArrayCollection();
    $this->nsfw = false;
}


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

/**
 * Set slug
 *
 * @param string $slug
 * @return Boom
 */
public function setSlug($slug)
{
    $this->slug = $slug;
    return $this;
}

/**
 * Get slug
 *
 * @return string
 */
public function getSlug()
{
    return $this->slug;
}

/**
 * Set title
 *
 * @param string $title
 * @return Boom
 */
public function setTitle($title)
{
    $this->title = $title;
    return $this;
}

/**
 * Get title
 *
 * @return string
 */
public function getTitle()
{
    return $this->title;
}

/**
 * Set summary
 *
 * @param text $summary
 * @return Boom
 */
public function setSummary($summary)
{
    $this->summary = $summary;
    return $this;
}

/**
 * Get summary
 *
 * @return text
 */
public function getSummary()
{
    return $this->summary;
}

/**
 * Set date_created
 *
 * @param datetime $dateCreated
 * @return Boom
 */
public function setDateCreated($dateCreated)
{
    $this->date_created = $dateCreated;
    return $this;
}

/**
 * Get date_created
 *
 * @return datetime
 */
public function getDateCreated()
{
    return $this->date_created;
}

/**
 * Set date_published
 *
 * @param datetime $datePublished
 * @return Boom
 */
public function setDatePublished($datePublished)
{
    $this->date_published = $datePublished;
    return $this;
}

/**
 * Get date_published
 *
 * @return datetime
 */
public function getDatePublished()
{
    return $this->date_published;
}

/**
 * Set nsfw
 *
 * @param boolean $nsfw
 * @return Boom
 */
public function setNsfw($nsfw)
{
    $this->nsfw = $nsfw;
    return $this;
}

/**
 * Get nsfw
 *
 * @return boolean
 */
public function getNsfw()
{
    return $this->nsfw;
}

/**
 * Set image
 *
 * @param Boom\Bundle\LibraryBundle\Entity\Image $image
 * @return Boom
 */
public function setImage(\Boom\Bundle\LibraryBundle\Entity\Image $image = null)
{
    $this->image = $image;
    return $this;
}

/**
 * Get image
 *
 * @return Boom\Bundle\LibraryBundle\Entity\Image
 */
public function getImage()
{
    return $this->image;
}

/**
 * Set user
 *
 * @param Boom\Bundle\LibraryBundle\Entity\User $user
 * @return Boom
 */
public function setUser(\Boom\Bundle\LibraryBundle\Entity\User $user = null)
{
    $this->user = $user;
    return $this;
}

/**
 * Get user
 *
 * @return Boom\Bundle\LibraryBundle\Entity\User
 */
public function getUser()
{
    return $this->user;
}

/**
 * Set parent
 *
 * @param Boom\Bundle\LibraryBundle\Entity\Boom $parent
 * @return Boom
 */
public function setParent(Boom $parent = null)
{
    $this->parent = $parent;
    return $this;
}

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

/**
 * Add children
 *
 * @param Boom\Bundle\LibraryBundle\Entity\Boom $children
 * @return Boom
 */
public function addChildren(\Boom\Bundle\LibraryBundle\Entity\Boom $children)
{
    $this->children[] = $children;
    return $this;
}

/**
 * Remove children
 *
 * @param <variableType$children
 */
public function removeChildren(\Boom\Bundle\LibraryBundle\Entity\Boom $children)
{
    $this->children->removeElement($children);
}

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

/**
 * Add categories
 *
 * @param Boom\Bundle\LibraryBundle\Entity\Category $categories
 * @return Boom
 */
public function addCategorie(\Boom\Bundle\LibraryBundle\Entity\Category $categories)
{
    $this->categories[] = $categories;
    return $this;
}

/**
 * Remove categories
 *
 * @param <variableType$categories
 */
public function removeCategorie(\Boom\Bundle\LibraryBundle\Entity\Category $categories)
{
    $this->categories->removeElement($categories);
}

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

/**
 * Set categories
 *
 * @return Boom
 */

public function setCategories(\Doctrine\Common\Collections\Collection $categories)
{
    return $this;
}


/**
 * Add elements
 *
 * @param Boom\Bundle\LibraryBundle\Entity\Boomelement $elements
 * @return Boom
 */
public function addElement(\Boom\Bundle\LibraryBundle\Entity\Boomelement $elements)
{

    $this->elements[] = $elements;
    if($elements->getBoom() == null || $elements->getBoom() !== $this){
        $elements->setBoom($this);
    }
    return $this;
}

/**
 * Remove elements
 *
 * @param <variableType$elements
 */
public function removeElement(\Boom\Bundle\LibraryBundle\Entity\Boomelement $elements)
{
    $this->elements->removeElement($elements);
    $elements->setBoom();
}

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



/**
 * Add tags
 *
 * @param Boom\Bundle\LibraryBundle\Entity\Tag $tags
 * @return Boom
 */
public function addTag(\Boom\Bundle\LibraryBundle\Entity\Tag $tags)
{
    $this->tags[] = $tags;
    return $this;
}

/**
 * Remove tags
 *
 * @param <variableType$tags
 */
public function removeTag(\Boom\Bundle\LibraryBundle\Entity\Tag $tags)
{
    $this->tags->removeElement($tags);
}

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

/**
 * @ORM\PrePersist()
 *
 */
public function prePersist(){
    $this->setDateCreated(new \DateTime());
}


public function hasCategories(ExecutionContext $context){
    if($this->getCategories()->count() == 0){
        $context->addViolationAtSubPath('categories','No categories selected');
    }

}

}

子元素= Boomelement

/**
 * @ORM\Entity
 * @ORM\Table(name="boomelement")
 */
class Boomelement extends DomainObject{

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string", length=140, nullable=true)
 */
public $title;

/**
 * @ORM\Column(type="text", nullable=true)
 */
protected $content;

/**
 * @Gedmo\SortablePosition
 * @ORM\Column(type="integer")
 */
protected $position;

/**
 * @Gedmo\SortableGroup
 * @ORM\ManyToOne(targetEntity="Boom", inversedBy="elements")
 * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
 **/
protected $boom;

/**
 * @ORM\ManyToOne(targetEntity="Image", inversedBy="image")
 * @ORM\JoinColumn(name="image_id", referencedColumnName="id")
 **/
protected $image;



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

/**
 * Set title
 *
 * @param string $title
 * @return Boomelement
 */
public function setTitle($title)
{
    $this->title = $title;
    return $this;
}

/**
 * Get title
 *
 * @return string
 */
public function getTitle()
{
    return $this->title;
}

/**
 * Set content
 *
 * @param text $content
 * @return Boomelement
 */
public function setContent($content)
{
    $this->content = $content;
    return $this;
}

/**
 * Get content
 *
 * @return text
 */
public function getContent()
{
    return $this->content;
}

/**
 * Set position
 *
 * @param integer $position
 * @return Boomelement
 */
public function setPosition($position)
{
    $this->position = $position;
    return $this;
}

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

/**
 * Set boom
 *
 * @param Boom\Bundle\LibraryBundle\Entity\Boom $boom
 * @return Boomelement
 */
public function setBoom(\Boom\Bundle\LibraryBundle\Entity\Boom $boom = null)
{
    $this->boom = $boom;
    //var_dump($boom);
    if($boom !== $this->boom){
        $boom->addElement($this);
    }

    return $this;
}

/**
 * Get boom
 *
 * @return Boom\Bundle\LibraryBundle\Entity\Boom
 */
public function getBoom()
{
    return $this->boom;
}

/**
 * Set image
 *
 * @param Boom\Bundle\LibraryBundle\Entity\Image $image
 * @return Boomelement
 */
public function setImage(\Boom\Bundle\LibraryBundle\Entity\Image $image = null)
{
    $this->image = $image;
    return $this;
}

/**
 * Get image
 *
 * @return Boom\Bundle\LibraryBundle\Entity\Image
 */
public function getImage()
{
    return $this->image;
}
}

当我使用空实体创建表单时,相关记录

public function createAction() {
    $entityEmpty = new Boom();
    $form = $this->createForm(new BoomType(),$entityEmpty);
    $request = $this->getRequest();
    $form->bind($request);
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entityEmpty);
        $em->flush();

        return $this->redirect($this->generateUrl('boom_show', array('id' => $entity->getId())));
    }else{
        //var_dump($form->getErrors());
        //var_dump(get_class_methods($form));
    }

    return $this->render('BoomBackBundle:Boom:new.html.php', array(
                'entity' => $entity,
                'form' => $form->createView(),
            ));
}
4

0 回答 0