I'm trying to make a One-to-Many/Many-to-one association in Symfony3.4 Doctrine ! i'm working on 2 entities [Single and Many] the "single" entity contains a "Many" entity and the "Many" entity contains an array collection of other "singles" entity. I've tried to create a new instance of single and a new instance of many and i assigned the many entity to the single before i've added many singles in many entity but the problem is when i try to query all the singles i got no singles added to the many entity ! This is my Many entity``
namespace AjaxBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Many
*
* @ORM\Table(name="many")
* @ORM\Entity(repositoryClass="AjaxBundle\Repository\ManyRepository")
*/
class Many
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToMany( targetEntity="AjaxBundle\Entity\Single",
mappedBy="single",cascade={"persist"})
* @var \Doctrine\Common\Collections\Collection
*/
public $multi;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Constructor
*/
public function __construct()
{
$this->multi = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add multi
*
* @param \AjaxBundle\Entity\Single $multi
*
* @return Many
*/
public function addMulti(\AjaxBundle\Entity\Single $multi)
{
$this->multi[] = $multi;
return $this;
}
/**
* Remove multi
*
* @param \AjaxBundle\Entity\Single $multi
*/
public function removeMulti(\AjaxBundle\Entity\Single $multi)
{
$this->multi->removeElement($multi);
}
/**
* Get multi
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getMulti()
{
return $this->multi;
}
}
And this is my Single entity
namespace AjaxBundle\Repository;
/**
* SingleRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
use AjaxBundle\Entity\Single;
use Doctrine\ORM\EntityNotFoundException;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\Query;
class SingleRepository extends \Doctrine\ORM\EntityRepository
{
public function fetch($id)
{
$manager=$this->getEntityManager();
$query=$manager->createQuery("SELECT u from AjaxBundle:Single u WHERE
u.id='$id'");
try{
return $query->getSingleResult(Query::HYDRATE_SIMPLEOBJECT);}
catch (NoResultException $xx)
{
die("impossible");
}
catch (NonUniqueResultException $zz)
{
die("impossible");
}
}
}
This is the controller (Testing sake)
public function showFriendAction()
{
$manager=$this->getDoctrine()->getManager();
$object2=$manager->getRepository(Single::class)->fetch(5);
$object3=$manager->getRepository(Single::class)->fetch(6);
$many=new Many();
$many->addMulti($object2);
$many->addMulti($object3);
$single=new Single();
$single->setNom("Somename");
$single->single=$many;
$manager->persist($many);
$manager->persist($single);
$manager->flush();
return new Response("");
}
And this is the fetch which i created to get Single objects (in order to add them to the Multi array)
public function fetch($id)
{
$manager=$this->getEntityManager();
$query=$manager->createQuery("SELECT u from AjaxBundle:Single u
WHERE
u.id='$id'");
try{
return $query->getSingleResult(Query::HYDRATE_SIMPLEOBJECT);}
catch (NoResultException $xx)
{
die("impossible");
}
catch (NonUniqueResultException $zz)
{
die("impossible");
}
}
This is my twig file to test the result
<html>
<head></head>
<body>
<center>
{% for x in data %}
<ul>{{ x.nom }}</ul>
{% if x.id==9%}
{% for m in x.single %}
<h2>{{m.multi.nom}}</h2>
{% endfor %}
{% endif %}
{% endfor %}
</center>
</body>
</html>
Any one can show me how to fix the problem ? Thanks a lot