title: Symfony2: One To Many Relationship
body: <p>Experiencing some serious problems in getting a OneToMany relationship to work. The error happens when trying to add "Children" to Beauties in Sonata Admin.</p>
  
  <p>Using:</p>
  
  <p>Symfony2
  Doctrine
  Sonata Admin</p>
  
  <p>Been trying to follow the guide at: <a href="http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-bidirectional" rel="nofollow noreferrer">http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-bidirectional</a></p>
  
  <p>and</p>
  
  <p><a href="http://sonata-project.org/bundles/doctrine-orm-admin/2-2/doc/reference/form_field_definition.html" rel="nofollow noreferrer">http://sonata-project.org/bundles/doctrine-orm-admin/2-2/doc/reference/form_field_definition.html</a></p>
  
  <p>Error:</p>
  
  <blockquote>
    <p>"CRITICAL - Uncaught PHP Exception
    Doctrine\ORM\ORMInvalidArgumentException: "A new entity was found
    through the relationship
    'Beautify\BeautiesBundle\Entity\Beauty#children' that was not
    configured to cascade persist operations for entity:
    Beautify\BeautiesBundle\Entity\Children@000000000eff91d5000000017f8ca53b.
    To solve this issue: Either explicitly call EntityManager#persist() on
    this unknown entity or configure cascade persist this association in
    the mapping for example @ManyToOne(..,cascade={"persist"}). If you
    cannot find out which entity causes the problem implement
    'Beautify\BeautiesBundle\Entity\Children#__toString()' to get a clue."
    at
    /Users/gmf/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/ORMInvalidArgumentException.php
    line 91 "</p>
  </blockquote>
  
  <p>.
  .</p>
  
  <p>BeautyAdmin.php</p>
  
  <hr>
  
  <pre><code>&lt;?php
  // src/Beautify/BeautiesBundle/Admin/BeautyAdmin.php
  
  namespace Beautify\BeautiesBundle\Admin;
  
  use Sonata\AdminBundle\Admin\Admin;
  use Sonata\AdminBundle\Datagrid\ListMapper;
  use Sonata\AdminBundle\Datagrid\DatagridMapper;
  use Sonata\AdminBundle\Form\FormMapper;
  
  use Knp\Menu\ItemInterface as MenuItemInterface;
  
  use Beautify\CategoryBundle\Entity\Category;
  use Beautify\CategoryBundle\Entity\Children;
  
  class BeautyAdmin extends Admin
  {
      // Fields to be shown on create/edit forms
      protected function configureFormFields(FormMapper $formMapper)
      {
          $formMapper
              -&gt;with('General')
                  -&gt;add('name', 'text', array('label' =&gt; 'Name'))
                  -&gt;add('content', 'textarea', array('label' =&gt; 'Content'))
                  -&gt;add('imageFile', 'file', array('label' =&gt; 'Image file', 'required' =&gt; false))
              -&gt;end()
              -&gt;with('Category')
                  -&gt;add('category', 'sonata_type_model', array('expanded' =&gt; true))
              -&gt;end()
              -&gt;with('Children')
  
                  -&gt;add('children', 'sonata_type_collection', array(), array(
                  'edit' =&gt; 'inline',
                  'inline' =&gt; 'table',
                  'sortable'  =&gt; 'position'
              ))
  
              -&gt;end()
  
  
  
  
          ;
      }
  
  
  
      // Fields to be shown on filter forms
      protected function configureDatagridFilters(DatagridMapper $datagridMapper)
      {
          $datagridMapper
              -&gt;add('name')
              -&gt;add('content')
              -&gt;add('category')
  
  
          ;
      }
  
      // Fields to be shown on lists
      protected function configureListFields(ListMapper $listMapper)
      {
          $listMapper
              -&gt;addIdentifier('name')
              -&gt;add('content')
              -&gt;add('category')
              -&gt;add('imageName')
              -&gt;add('_action', 'actions', array(
                  'actions' =&gt; array(
                      'show' =&gt; array(),
                      'edit' =&gt; array(),
                      'delete' =&gt; array(),
                  )
              ))
          ;
      }
  }
  </code></pre>
  
  <p>Beauty.php Entity</p>
  
  <hr>
  
  <pre><code>&lt;?php
  
  // src/Beautify/BeautiesBundle/Entity/Beauty.php
  
  namespace Beautify\BeautiesBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\HttpFoundation\File\UploadedFile;
  use Symfony\Component\HttpFoundation\File\File;
  use Vich\UploaderBundle\Mapping\Annotation as Vich;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /** 
  * @ORM\Entity
  * @ORM\Table(name="beauties")
  * @ORM\HasLifecycleCallbacks
  * @Vich\Uploadable
  */
  
   class Beauty
   {
  
       /**
       * @ORM\OneToMany(targetEntity="Children", mappedBy="beauties")
       **/
      private $children;
  
      public function __construct()
      {
          $this-&gt;children = new ArrayCollection();
      }
  
  
  
  
  
      /**
      * @ORM\Column(type="integer")
      * @ORM\Id
      * @ORM\GeneratedValue(strategy="AUTO")
      */
      protected $id;
  
      /**
      * @ORM\Column(type="string", length=100)
      */
  
      protected $name;
  
      /**
      * @ORM\Column(type="text")
      */
  
      protected $content;  
  
  
  
      /**
       * @Vich\UploadableField(mapping="beauty_image", fileNameProperty="imageName")
       * @var File $imageFile
       */
      protected $imageFile;
  
      /**
       * @ORM\Column(type="string", length=255, nullable=true, name="image_name")
       * @var string $imageName
       */
      protected $imageName;
  
  
  
      /**
      * @ORM\ManyToOne(targetEntity="Category", inversedBy="beauties")
      * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
      */
      protected $category;
  
  
  
  
  
  /**
       * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
       * of 'UploadedFile' is injected into this setter to trigger the  update. If this
       * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
       * must be able to accept an instance of 'File' as the bundle will inject one here
       * during Doctrine hydration.
       *
       * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile
       */
      public function setImageFile(File $image = null)
      {
          $this-&gt;imageFile = $image;
  
          if ($image) {
              // It is required that at least one field changes if you are using doctrine
              // otherwise the event listeners won't be called and the file is lost
              $this-&gt;updatedAt = new \DateTime('now');
          }
      }
  
      /**
       * @return File
       */
      public function getImageFile()
      {
          return $this-&gt;imageFile;
      }
  
      /**
       * @param string $imageName
       */
      public function setImageName($imageName)
      {
          $this-&gt;imageName = $imageName;
      }
  
      /**
       * @return string
       */
      public function getImageName()
      {
          return $this-&gt;imageName;
      }
  
  
  
  
  
  
      public function __toString()
      {
      return ($this-&gt;getName()) ? : '';
      }
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set name
       *
       * @param string $name
       * @return Beauty
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string 
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Set content
       *
       * @param string $content
       * @return Beauty
       */
      public function setContent($content)
      {
          $this-&gt;content = $content;
  
          return $this;
      }
  
      /**
       * Get content
       *
       * @return string 
       */
      public function getContent()
      {
          return $this-&gt;content;
      }
  
      /**
       * Set category
       *
       * @param \Beautify\BeautiesBundle\Entity\Category $category
       * @return Beauty
       */
      public function setCategory(\Beautify\BeautiesBundle\Entity\Category $category = null)
      {
          $this-&gt;category = $category;
  
          return $this;
      }
  
      /**
       * Get category
       *
       * @return \Beautify\BeautiesBundle\Entity\Category 
       */
      public function getCategory()
      {
          return $this-&gt;category;
      }
  
  
  
      /**
       * Set children
       *
       * @param \Beautify\BeautiesBundle\Entity\Children $children
       * @return Beauty
       */
      public function setChildren(\Beautify\BeautiesBundle\Entity\Children $children = null)
      {
          $this-&gt;children = $children;
  
          return $this;
      }
  
      /**
       * Get children
       *
       * @return \Beautify\BeautiesBundle\Entity\Children 
       */
      public function getChildren()
      {
          return $this-&gt;children;
      }
  
  
  
  
      /**
       * Add children
       *
       * @param \Beautify\BeautiesBundle\Entity\Children $children
       * @return Beauty
       */
      public function addChild(\Beautify\BeautiesBundle\Entity\Children $children)
      {
          $this-&gt;children[] = $children;
  
          return $this;
      }
  
      /**
       * Remove children
       *
       * @param \Beautify\BeautiesBundle\Entity\Children $children
       */
      public function removeChild(\Beautify\BeautiesBundle\Entity\Children $children)
      {
          $this-&gt;children-&gt;removeElement($children);
      }
  }
  </code></pre>
  
  <p>Children.php Entity</p>
  
  <hr>
  
  <pre><code>&lt;?php
  
  
  
  namespace Beautify\BeautiesBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  
  /**
   * Children
   *
  * @ORM\Table(name="children")
   * @ORM\Entity
   */
  class Children
  {
  
      /**
       * @ORM\ManyToOne(targetEntity="Beauty", inversedBy="children")
       * @ORM\JoinColumn(name="beauty_id", referencedColumnName="id")
       **/
  
      private $beauties;
  
  
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="image", type="string", length=255)
       */
      private $image;
  
      /**
       * @var string
       *
       * @ORM\Column(name="content", type="text")
       */
      private $content;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="priority", type="integer")
       */
      private $priority;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="parent_id", type="integer")
       */
      private $parentId;
  
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set image
       *
       * @param string $image
       * @return Children
       */
      public function setImage($image)
      {
          $this-&gt;image = $image;
  
          return $this;
      }
  
      /**
       * Get image
       *
       * @return string 
       */
      public function getImage()
      {
          return $this-&gt;image;
      }
  
      /**
       * Set content
       *
       * @param string $content
       * @return Children
       */
      public function setContent($content)
      {
          $this-&gt;content = $content;
  
          return $this;
      }
  
      /**
       * Get content
       *
       * @return string 
       */
      public function getContent()
      {
          return $this-&gt;content;
      }
  
      /**
       * Set priority
       *
       * @param integer $priority
       * @return Children
       */
      public function setPriority($priority)
      {
          $this-&gt;priority = $priority;
  
          return $this;
      }
  
      /**
       * Get priority
       *
       * @return integer 
       */
      public function getPriority()
      {
          return $this-&gt;priority;
      }
  
      /**
       * Set parentId
       *
       * @param integer $parentId
       * @return Children
       */
      public function setParentId($parentId)
      {
          $this-&gt;parentId = $parentId;
  
          return $this;
      }
  
      /**
       * Get parentId
       *
       * @return integer 
       */
      public function getParentId()
      {
          return $this-&gt;parentId;
      }
  
  
  
  
  
      /**
       * Set beauties
       *
       * @param \Beautify\BeautiesBundle\Entity\Beauty $beauties
       * @return Children
       */
      public function setBeauties(\Beautify\BeautiesBundle\Entity\Beauty $beauties = null)
      {
          $this-&gt;beauties = $beauties;
  
          return $this;
      }
  
      /**
       * Get beauties
       *
       * @return \Beautify\BeautiesBundle\Entity\Beauty 
       */
      public function getBeauties()
      {
          return $this-&gt;beauties;
      }
  }
  </code></pre>
  
  <p>Very thankful for any help...</p>