I have three entities a user, a category and and event, a user can be intressted in many categories and an event can have a categorie.
User.php:
namespace Mql14\\mqlmeBundle\\Entity;
use Doctrine\\ORM\\Mapping as ORM;
/**
* Mql14\mqlmeBundle\Entity\User
*
* @ORM\Table(name="user")
* @ORM\Entity
*/
class User
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $nom
*
* @ORM\Column(name="nom", type="string", length=45, nullable=true)
*/
private $nom;
/**
* @var string $prenom
*
* @ORM\Column(name="prenom", type="string", length=45, nullable=true)
*/
private $prenom;
/**
* @var string $login
*
* @ORM\Column(name="login", type="string", length=45, nullable=true)
*/
private $login;
/**
* @var string $pass
*
* @ORM\Column(name="pass", type="string", length=45, nullable=true)
*/
private $pass;
/**
* @var Categorie
*
* @ORM\ManyToMany(targetEntity="Categorie", inversedBy="user")
* @ORM\JoinTable(name="interesse",
* joinColumns={
* @ORM\JoinColumn(name="User_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="Categorie_id", referencedColumnName="id")
* }
* )
*/
private $categorie;
public function __construct()
{
$this->categorie = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $nom
*/
public function setNom($nom)
{
$this->nom = $nom;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set prenom
*
* @param string $prenom
*/
public function setPrenom($prenom)
{
$this->prenom = $prenom;
}
/**
* Get prenom
*
* @return string
*/
public function getPrenom()
{
return $this->prenom;
}
/**
* Set login
*
* @param string $login
*/
public function setLogin($login)
{
$this->login = $login;
}
/**
* Get login
*
* @return string
*/
public function getLogin()
{
return $this->login;
}
/**
* Set pass
*
* @param string $pass
*/
public function setPass($pass)
{
$this->pass = $pass;
}
/**
* Get pass
*
* @return string
*/
public function getPass()
{
return $this->pass;
}
/**
* Add categorie
*
* @param Mql14\mqlmeBundle\Entity\Categorie $categorie
*/
public function addCategorie(\Mql14\mqlmeBundle\Entity\Categorie $categorie)
{
$this->categorie[] = $categorie;
}
/**
* Get categorie
*
* @return Doctrine\Common\Collections\Collection
*/
public function getCategorie()
{
return $this->categorie;
}
}
Categorie.php:
namespace Mql14\mqlmeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Mql14\mqlmeBundle\Entity\Categorie
*
* @ORM\Table(name="categorie")
* @ORM\Entity
*/
class Categorie
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $nomcat
*
* @ORM\Column(name="nomCat", type="string", length=45, nullable=true)
*/
private $nomcat;
/**
* @var string $photo
*
* @ORM\Column(name="photo", type="string", length=45, nullable=true)
*/
private $photo;
/**
* @var $evenement
*
* @ORM\OneToMany(targetEntity="Evenement", mappedBy="categorie")
*/
private $evenement;
/**
* @var User
*
* @ORM\ManyToMany(targetEntity="User", mappedBy="categorie")
*/
private $user;
public function __construct()
{
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nomcat
*
* @param string $nomcat
*/
public function setNomcat($nomcat)
{
$this->nomcat = $nomcat;
}
/**
* Get nomcat
*
* @return string
*/
public function getNomcat()
{
return $this->nomcat;
}
/**
* Set photo
*
* @param string $photo
*/
public function setPhoto($photo)
{
$this->photo = $photo;
}
/**
* Get photo
*
* @return string
*/
public function getPhoto()
{
return $this->photo;
}
/**
* Add user
*
* @param Mql14\mqlmeBundle\Entity\User $user
*/
public function addUser(\Mql14\mqlmeBundle\Entity\User $user)
{
$this->user[] = $user;
}
/**
* Get user
*
* @return Doctrine\Common\Collections\Collection
*/
public function getUser()
{
return $this->user;
}
/**
* Add evenement
*
* @param Mql14\mqlmeBundle\Entity\Categorie $evenement
*/
public function addEvenement(\Mql14\mqlmeBundle\Entity\Evenement $evenement)
{
$this->evenement[] = $evenement;
}
/**
* Get evenement
*
* @return Doctrine\Common\Collections\Collection
*/
public function getEvenement()
{
return $this->evenement;
}
}
Evenement.php
<?php
namespace Mql14\mqlmeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Mql14\mqlmeBundle\Entity\Evenement
*
* @ORM\Table(name="evenement")
* @ORM\Entity
*/
class Evenement
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $nom
*
* @ORM\Column(name="nom", type="string", length=45, nullable=true)
*/
private $nom;
/**
* @var datetime $date
*
* @ORM\Column(name="date", type="datetime", nullable=true)
*/
private $date;
/**
* @var string $description
*
* @ORM\Column(name="description", type="string", length=400, nullable=true)
*/
private $description;
/**
* @var integer $ticket
*
* @ORM\Column(name="Ticket", type="integer", nullable=true)
*/
private $ticket;
/**
* @var User
*
* @ORM\ManyToMany(targetEntity="User", mappedBy="evenement")
*/
private $user;
/**
* @var Categorie
*
* @ORM\ManyToOne(targetEntity="Categorie", inversedBy="evenement")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="categorie_id", referencedColumnName="id")
* })
*/
private $categorie;
/**
* @var Lieu
*
* @ORM\ManyToOne(targetEntity="Lieu")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="lieu_id", referencedColumnName="id")
* })
*/
private $lieu;
public function __construct()
{
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $nom
*/
public function setNom($nom)
{
$this->nom = $nom;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set date
*
* @param datetime $date
*/
public function setDate($date)
{
$this->date = $date;
}
/**
* Get date
*
* @return datetime
*/
public function getDate()
{
return $this->date;
}
/**
* Set description
*
* @param string $description
*/
public function setDescription($description)
{
$this->description = $description;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set ticket
*
* @param integer $ticket
*/
public function setTicket($ticket)
{
$this->ticket = $ticket;
}
/**
* Get ticket
*
* @return integer
*/
public function getTicket()
{
return $this->ticket;
}
/**
* Add user
*
* @param Mql14\mqlmeBundle\Entity\User $user
*/
public function addUser(\Mql14\mqlmeBundle\Entity\User $user)
{
$this->user[] = $user;
}
/**
* Get user
*
* @return Doctrine\Common\Collections\Collection
*/
public function getUser()
{
return $this->user;
}
/**
* Set categorie
*
* @param Mql14\mqlmeBundle\Entity\Categorie $categorie
*/
public function setCategorie(\Mql14\mqlmeBundle\Entity\Categorie $categorie)
{
$this->categorie = $categorie;
}
/**
* Get categorie
*
* @return Mql14\mqlmeBundle\Entity\Categorie
*/
public function getCategorie()
{
return $this->categorie;
}
/**
* Set lieu
*
* @param Mql14\mqlmeBundle\Entity\Lieu $lieu
*/
public function setLieu(\Mql14\mqlmeBundle\Entity\Lieu $lieu)
{
$this->lieu = $lieu;
}
/**
* Get lieu
*
* @return Mql14\mqlmeBundle\Entity\Lieu
*/
public function getLieu()
{
return $this->lieu;
}
public function getCategorieId(\Mql14\mqlmeBundle\Entity\Categorie $categorie)
{
$idc= $categorie->getId();
return $idc;
}
}
Now the action in my controller that would allow me to display events(evenements) after getting the user's id and the category he's interessted in is(the argument $id is the user's id):
public function interetAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$users = $em->getRepository('Mql14mqlmeBundle:User')->find($id);
foreach($users->getCategorie() as $cat) {
$events=$cat->getEvenement();
}
return $this->container->get('templating')->renderResponse('Mql14mqlmeBundle:Event:interet.html.twig', array(
'categoriesEvents' => $events,
));
}
But with this action even if the user is intressted in many categories, the events that are displayed are of only one categories, so what should I do to be able to dispaly all the events of all the categories that a certain user is intressted in
in your controller at line: $events=$cat->getEvenement();
change it to: $events[]=$cat->getEvenement();
beacause in foreach loop you override $events variable each time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.