Skip to content

Instantly share code, notes, and snippets.

@dionisos2
Forked from anonymous/Concept.php
Last active December 14, 2015 22:59
Show Gist options
  • Save dionisos2/5162527 to your computer and use it in GitHub Desktop.
Save dionisos2/5162527 to your computer and use it in GitHub Desktop.
If i remove $this->em->flush($conceptNew); i get: Entity of type Ukratio\TrouveToutBundle\Entity\ConceptConcept has identity through a foreign entity Ukratio\TrouveToutBundle\Entity\Concept, however this entity has no identity itself. You have to call EntityManager#persist() on the related entity and make sure that an identifier was generated be…
<?php
namespace Ukratio\TrouveToutBundle\Entity;
use /*all the uses*/
/**
* Ukratio\TrouveToutBundle\Entity\Concept
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Ukratio\TrouveToutBundle\Entity\ConceptRepository")
* @UniqueEntity("name")
*/
class Concept
{
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Ukratio\TrouveToutBundle\Entity\ConceptConcept", mappedBy="moreSpecific", cascade={"persist"}, orphanRemoval=true)
*/
private $moreGeneralConceptConcepts;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Ukratio\TrouveToutBundle\Entity\ConceptConcept", mappedBy="moreGeneral", cascade={"persist"}, orphanRemoval=true)
*/
private $moreSpecificConceptConcepts;
public function __construct()
{
$this->moreGeneralConceptConcepts = new ArrayCollection();
$this->moreSpecificConceptConcepts = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add moreGeneralConceptConcepts
*
* @param \Ukratio\TrouveToutBundle\Entity\ConceptConcept $moreGeneralConceptConcepts
* @return ConceptConcept
*
*/
public function addMoreGeneralConceptConcept(ConceptConcept $moreGeneralConceptConcept)
{
$moreGeneralConceptConcept->setMoreSpecific($this);
$this->moreGeneralConceptConcepts[] = $moreGeneralConceptConcept;
return $this;
}
public function removeMoreGeneralConceptConcept(ConceptConcept $moreGeneralConceptConcept)
{
$this->moreGeneralConceptConcepts->removeElement($moreGeneralConceptConcept);
}
public function removeMoreSpecificConceptConcept(ConceptConcept $moreSpecificConceptConcept)
{
$this->moreSpecificConceptConcepts->removeElement($moreSpecificConceptConcept);
}
/**
* Get moreGeneralConceptConcepts
*
* @return \Doctrine\Common\Collections\Collection
*
*/
public function getMoreGeneralConceptConcepts()
{
return $this->moreGeneralConceptConcepts;
}
}
<?php
namespace Ukratio\TrouveToutBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Ukratio\TrouveToutBundle\Entity\Concept;
/**
* ConceptConcept
*
* @ORM\Table()
* @ORM\Entity
*/
class ConceptConcept
{
/**
* @var Ukratio\TrouveToutBundle\Entity\Concept
*
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Ukratio\TrouveToutBundle\Entity\Concept", inversedBy="moreSpecificConcepts")
*/
private $moreGeneral;
/**
* @var Ukratio\TrouveToutBundle\Entity\Concept
*
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Ukratio\TrouveToutBundle\Entity\Concept", inversedBy="moreGeneralConcepts")
*/
private $moreSpecific;
public function linkConcepts(Concept $moreSpecific, Concept $moreGeneral)
{
$this->setMoreGeneral($moreGeneral);
$this->setMoreSpecific($moreSpecific);
}
/**
* Set moreGeneral
*
* @param \Ukratio\TrouveToutBundle\Entity\Concept $moreGeneral
* @return ConceptConcept
*
* @codeCoverageIgnore
*/
public function setMoreGeneral(Concept $moreGeneral)
{
$this->moreGeneral = $moreGeneral;
return $this;
}
/**
* Get moreGeneral
*
* @return \Ukratio\TrouveToutBundle\Entity\Concept
*
* @codeCoverageIgnore
*/
public function getMoreGeneral()
{
return $this->moreGeneral;
}
/**
* Set moreSpecific
*
* @param \Ukratio\TrouveToutBundle\Entity\Concept $moreSpecific
* @return ConceptConcept
*
* @codeCoverageIgnore
*/
public function setMoreSpecific(Concept $moreSpecific)
{
$this->moreSpecific = $moreSpecific;
return $this;
}
/**
* Get moreSpecific
*
* @return \Ukratio\TrouveToutBundle\Entity\Concept
*
* @codeCoverageIgnore
*/
public function getMoreSpecific()
{
return $this->moreSpecific;
}
}
public function testAction(Request $request)
{
$this->em = $this->getDoctrine()->getManager();
$this->repo = $this->em->getRepository('TrouveToutBundle:Concept');
$conceptMouchoir = $this->repo->findOneByName('mouchoir');
$conceptNew = new Concept();
$conceptNew->setName('conceptNew');
$this->em->persist($conceptNew);
$this->em->flush($conceptNew);
$conceptConcept = new ConceptConcept();
$conceptConcept->setMoreGeneral($conceptMouchoir);
$conceptNew->addMoreGeneralConceptConcept($conceptConcept);
$this->em->persist($conceptNew);
$this->em->flush($conceptNew);
return new Response("ok");
}
@dionisos2
Copy link
Author

If i remove "$this->em->flush($conceptNew); " in controlleur.php line 10, i get:

Entity of type Ukratio\TrouveToutBundle\Entity\ConceptConcept has identity through a foreign entity Ukratio\TrouveToutBundle\Entity\Concept, however this entity has no identity itself. You have to call EntityManager#persist() on the related entity and make sure that an identifier was generated before trying to persist 'Ukratio\TrouveToutBundle\Entity\ConceptConcept'. In case of Post Insert ID Generation (such as MySQL Auto-Increment or PostgreSQL SERIAL) this means you have to call EntityManager#flush() between both persist operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment