title: Unable to save (A new entity was found through the relationship)
body: <p>Error Message: </p>
  
  <blockquote>
    <p>"A new entity was found through the relationship
    'AppBundle\Entity\Category#products' that was not configured to
    cascade persist operations for entity:
    AppBundle\Entity\Product@00000000133d712400000000104ed306. 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
    'AppBundle\Entity\Product#__toString()' to get a clue."</p>
  </blockquote>
  
  <p>/AppBundle/Entity/Product</p>
  
  <pre><code>class Product
  {
  /**
   * @ORM\Column(type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
  /**
   * @ORM\Column(type="string", length=100)
   */
  private $name;
  
  /**
   * @ORM\Column(type="decimal", scale=2)
   */
  private $price;
  
  /**
   * @ORM\Column(type="text")
   */
  private $description;
  
  /**
   * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
   * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
   */
  private $category;
  
  /**
   * Get id
   *
   * @return integer
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set name
   *
   * @param string $name
   *
   * @return Product
   */
  public function setName($name)
  {
      $this-&gt;name = $name;
  
      return $this;
  }
  
  /**
   * Get name
   *
   * @return string
   */
  public function getName()
  {
      return $this-&gt;name;
  }
  
  /**
   * Set price
   *
   * @param string $price
   *
   * @return Product
   */
  public function setPrice($price)
  {
      $this-&gt;price = $price;
  
      return $this;
  }
  
  /**
   * Get price
   *
   * @return string
   */
  public function getPrice()
  {
      return $this-&gt;price;
  }
  
  /**
   * Set description
   *
   * @param string $description
   *
   * @return Product
   */
  public function setDescription($description)
  {
      $this-&gt;description = $description;
  
      return $this;
  }
  
  /**
   * Get description
   *
   * @return string
   */
  public function getDescription()
  {
      return $this-&gt;description;
  }
  
  /**
   * Set category
   *
   * @param \AppBundle\Entity\Category $category
   *
   * @return Product
   */
  public function setCategory(\AppBundle\Entity\Category $category = null)
  {
      $this-&gt;category = $category;
  
      return $this;
  }
  
  /**
   * Get category
   *
   * @return \AppBundle\Entity\Category
   */
  public function getCategory()
  {
      return $this-&gt;category;
  }
  }
  </code></pre>
  
  <p>/AppBundle/Entity/Category</p>
  
  <pre><code>class Category
  {
  /**
   * @var int
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
  /**
   * @var string
   *
   * @ORM\Column(name="name", type="string", length=255)
   */
  protected $name;
  
  /**
   * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
   */
  protected $products;
  
  public function __construct()
  {
      $this-&gt;products = new ArrayCollection();
  }
  
  
  /**
   * Get id
   *
   * @return int
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set name
   *
   * @param string $name
   *
   * @return Category
   */
  public function setName($name)
  {
      $this-&gt;name = $name;
  
      return $this;
  }
  
  /**
   * Get name
   *
   * @return string
   */
  public function getName()
  {
      return $this-&gt;name;
  }
  
  /**
   * Add product
   *
   * @param \AppBundle\Entity\Product $product
   *
   * @return Category
   */
  public function addProduct(\AppBundle\Entity\Product $product)
  {
      $this-&gt;products[] = $product;
  
      return $this;
  }
  
  /**
   * Remove product
   *
   * @param \AppBundle\Entity\Product $product
   */
  public function removeProduct(\AppBundle\Entity\Product $product)
  {
      $this-&gt;products-&gt;removeElement($product);
  }
  
  /**
   * Get products
   *
   * @return \Doctrine\Common\Collections\Collection
   */
  public function getProducts()
  {
      return $this-&gt;products;
  }
  }
  </code></pre>
  
  <p>/AppBundle/Form/CategoryType</p>
  
  <pre><code>class CategoryType extends AbstractType
  {
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder-&gt;add('name');
      $builder-&gt;add('products', CollectionType::class, array(
          'entry_type' =&gt; ProductType::class
      ));
  }
  
  public function configureOptions(OptionsResolver $resolver)
  {
      $resolver-&gt;setDefaults(array(
         'data_class' =&gt; 'AppBundle\Entity\Category'
      ));
  }
  
  public function getName()
  {
      return 'category';
  }
  }
  </code></pre>
  
  <p>/AppBundle/Form/ProductType</p>
  
  <pre><code>class ProductType extends AbstractType
  {
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder-&gt;add('name')
              -&gt;add('price')
              -&gt;add('description');
  
  }
  
  public function configureOptions(OptionsResolver $resolver)
  {
      $resolver-&gt;setDefaults(array(
         'data_class' =&gt; 'AppBundle\Entity\Product',
      ));
  }
  
  public function getName()
  {
      return 'app_bundle_product_type';
  }
  }
  </code></pre>
  
  <p>/AppBundle/Controller/CategoryController</p>
  
  <pre><code>class CategoryController extends Controller
  {
  /**
   * @Route("/category/new", name="category_new")
   *
   */
  public function newAction(Request $request)
  {
      $category = new Category();
  
      $product = new Product();
      $category-&gt;getProducts()-&gt;add($product);
  
      $form = $this-&gt;createForm(CategoryType::class, $category);
  
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isValid() &amp;&amp; $form-&gt;isSubmitted()) {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;persist($category);
          $em-&gt;flush();
      }
  
      return $this-&gt;render('category/new.html.twig', array(
          'form' =&gt; $form-&gt;createView(),
      ));
  }
  }
  </code></pre>
  
  <p>/category/new.html.twig</p>
  
  <pre><code>{% extends 'base.html.twig' %}
  
  {% block body %}
  
  {{ form_start(form) }}
  {{ form_widget(form) }}
  &lt;button type="submit" class="btn btn-primary"&gt;Save&lt;/button&gt;
  {{ form_end(form) }}
  
  {% endblock %}
  </code></pre>