我有三个实体,一个用户,一个类别和一个事件,一个用户可以在许多类别中被强制插入,一个事件可以有一个类别。
user.php的:
命名空间Mql14 \ mqlmeBundle \ Entity;
将Doctrine \ ORM \ Mapping用作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;
}
}
现在我的控制器中的动作允许我在获得用户的id和他所关注的类别后显示事件(evenements)(参数$ id是用户&#39; 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,
));
}
但即使用户在许多类别中被强制执行此操作,显示的事件也只有一个类别,因此我应该怎么做才能显示特定用户所有类别的所有事件inressted in
答案 0 :(得分:0)
在您的控制器中:
$events=$cat->getEvenement();
将其更改为:
$events[]=$cat->getEvenement();
在foreach循环中,每次都覆盖$ events变量。