1
votes

I created two entities automatically ( using this manual http://symfony.com/doc/2.8/doctrine/reverse_engineering.html) based on ER model generated in Workbench. My intention was to create one-to-one relationship but annotation show it is one-to-many relationship. I created also embeed forms. I would like to insert client and new adress to database. I still get an error:

A new entity was found through the relationship 'UlaBundle\Entity\Client#adres' that was not configured to cascade persist operations for entity: qqq. 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"}).

Error is shown even if i set @ManyToOne(..,cascade={"persist"}) and __toString function. What is the problem? Please help. Below my code:

///Client Entity

class Client
{
/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=45, nullable=true)
 */
private $name;

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

/**
 * @var \UlaBundle\Entity\Adres
 *
 * @ORM\ManyToOne(targetEntity="UlaBundle\Entity\Adres", cascade= {"persist"})
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="adres_id", referencedColumnName="id")
 * })
 */
private $adres;

/// Adres Entity

class Adres
{
/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=45, nullable=true)
 */
private $name;

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

///Controller

/**
 * @Route("/client", name="client")
 */
public function clientAction(Request $request) {
    $c = new Client();
    $form = $this->createForm(ClientType::class,$c);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $m = $this->getDoctrine()->getManager();
        $m->persist($c);
        $m->flush();

        return new Response('Added');
    }
    return $this->render('UlaBundle:Default:client_form.html.twig', array('form' => $form->createView()));
}
1

1 Answers

0
votes

I think your problem come from the blank space in cascade= {"persist"}, you should remove it

/**
 * @var \UlaBundle\Entity\Adres
 *
 * @ORM\ManyToOne(targetEntity="UlaBundle\Entity\Adres", cascade={"persist"})
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="adres_id", referencedColumnName="id")
 * })
 */
private $adres;