我在Symfony 2.8.15上,我对一对多的学说关系有疑问。
我有一个实体InvitesGroupe和一个实体预订。 InvitesGroupe可以有一个或多个预订。因此,有一个hasReservations()
方法。
它有效,我的应用程序正在生产一个月。但最近我看到了一个巨大的结果:并未显示所有可能性。我有一个InviteGroupe,它在数据库中有43个预留,除了日期之外都有相同的格式,但只显示了42个。
如果尝试从分析器查询,我有43个预订。
我尝试了很多调试,但我不知道追加什么。
(拥有)InvitesGroupe实体:
<?php
namespace Ipb\AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* InvitesGroupe
*
* @ORM\Table(name="invites_groupe", indexes={@ORM\Index(name="FK_invites_groupe_id_service", columns={"id_service"})})
* @ORM\Entity(repositoryClass="Ipb\AppBundle\EntityRepository\InvitesGroupeRepository")
*/
class InvitesGroupe
{
/**
* @var integer
*
* @ORM\Column(name="id_invite", type="integer", precision=0, scale=0, nullable=false, unique=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idInvite;
/**
* @var integer
*
* @ORM\Column(name="nombre", type="integer", precision=0, scale=0, nullable=true, unique=false)
*/
private $nombre;
/**
* @var string
*
* @ORM\Column(name="nom_reservation", type="string", length=30, precision=0, scale=0, nullable=true, unique=false)
*
* @Assert\NotBlank()
* @Assert\Length(max = 30)
*
*/
private $nomReservation;
/**
* @var string
*
* @ORM\Column(name="commentaire", type="text", length=65535, precision=0, scale=0, nullable=true, unique=false)
*
* @Assert\NotBlank()
*
*/
private $commentaire;
/**
* @var boolean
*
* @ORM\Column(name="repas", type="boolean", precision=0, scale=0, nullable=true, unique=false)
*/
private $repas = 1;
/**
* @var boolean
*
* @ORM\Column(name="repas_offert", type="boolean", precision=0, scale=0, nullable=false, unique=false)
*/
private $repasOffert = false;
/**
* @var integer
*
* @ORM\Column(name="place_table", type="integer", precision=0, scale=0, nullable=true, unique=false)
*
* @Assert\NotBlank()
*
*/
private $placeTable;
/**
* @var boolean
*
* @ORM\Column(name="invite_visible", type="boolean", precision=0, scale=0, nullable=false, unique=false)
*/
private $inviteVisible = true;
/**
* @var \Ipb\AppBundle\Entity\Users
*
* @ORM\ManyToOne(targetEntity="Ipb\AppBundle\Entity\Users")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_user", referencedColumnName="id_user", nullable=true)
* })
*/
private $idUser;
/**
* @var boolean
*
* @ORM\Column(name="dessert", type="boolean", precision=0, scale=0, nullable=false, unique=false)
*/
private $dessert = false;
/**
* @var boolean
*
* @ORM\Column(name="wine", type="boolean", precision=0, scale=0, nullable=false, unique=false)
*/
private $wine = false;
/**
* @var boolean
*
* @ORM\Column(name="coffee", type="boolean", precision=0, scale=0, nullable=false, unique=false)
*/
private $coffee = false;
/**
* @var boolean
*
* @ORM\Column(name="invite_temporaire", type="boolean", precision=0, scale=0, nullable=true, unique=false)
*/
private $inviteTemporaire;
/**
* @var \Ipb\AppBundle\Entity\Services
*
* @ORM\ManyToOne(targetEntity="Ipb\AppBundle\Entity\Services")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_service", referencedColumnName="id_service", nullable=true)
* })
*/
private $idService;
/**
*
* @ORM\OneToMany(targetEntity="Ipb\AppBundle\Entity\Reservations", mappedBy="invitesGroupe", cascade={"persist","remove"})
*/
private $reservations;
/**
* Get idInvite
*
* @return integer
*/
public function getIdInvite()
{
return $this->idInvite;
}
/**
* Set nombre
*
* @param integer $nombre
* @return InvitesGroupe
*/
public function setNombre($nombre)
{
$this->nombre = $nombre;
return $this;
}
/**
* Get nombre
*
* @return integer
*/
public function getNombre()
{
return $this->nombre;
}
/**
* Set nomReservation
*
* @param string $nomReservation
* @return InvitesGroupe
*/
public function setNomReservation($nomReservation)
{
$this->nomReservation = $nomReservation;
return $this;
}
/**
* Get nomReservation
*
* @return string
*/
public function getNomReservation()
{
return $this->nomReservation;
}
/**
* Set commentaire
*
* @param string $commentaire
* @return InvitesGroupe
*/
public function setCommentaire($commentaire)
{
$this->commentaire = $commentaire;
return $this;
}
/**
* Get commentaire
*
* @return string
*/
public function getCommentaire()
{
return $this->commentaire;
}
/**
* Set repas
*
* @param boolean $repas
* @return InvitesGroupe
*/
public function setRepas($repas)
{
$this->repas = $repas;
return $this;
}
/**
* Get repas
*
* @return boolean
*/
public function getRepas()
{
return $this->repas;
}
/**
* Set repasOffert
*
* @param boolean $repasOffert
* @return InvitesGroupe
*/
public function setRepasOffert($repasOffert)
{
$this->repasOffert = $repasOffert;
return $this;
}
/**
* Get repasOffert
*
* @return boolean
*/
public function getRepasOffert()
{
return $this->repasOffert;
}
/**
* Set placeTable
*
* @param integer $placeTable
* @return InvitesGroupe
*/
public function setPlaceTable($placeTable)
{
$this->placeTable = $placeTable;
return $this;
}
/**
* Get placeTable
*
* @return integer
*/
public function getPlaceTable()
{
return $this->placeTable;
}
/**
* Set inviteVisible
*
* @param boolean $inviteVisible
* @return InvitesGroupe
*/
public function setInviteVisible($inviteVisible)
{
$this->inviteVisible = $inviteVisible;
return $this;
}
/**
* Get inviteVisible
*
* @return boolean
*/
public function getInviteVisible()
{
return $this->inviteVisible;
}
/**
* Set dessert
*
* @param boolean $dessert
* @return InvitesGroupe
*/
public function setDessert($dessert)
{
$this->dessert = $dessert;
return $this;
}
/**
* Get dessert
*
* @return boolean
*/
public function getDessert()
{
return $this->dessert;
}
/**
* Set idService
*
* @param \Ipb\AppBundle\Entity\Services $idService
* @return InvitesGroupe
*/
public function setIdService(\Ipb\AppBundle\Entity\Services $idService = null)
{
$this->idService = $idService;
return $this;
}
/**
* Get idService
*
* @return \Ipb\AppBundle\Entity\Services
*/
public function getIdService()
{
return $this->idService;
}
/**
* Constructor
*/
public function __construct()
{
$this->reservations = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add reservations
*
* @param \Ipb\AppBundle\Entity\Reservations $reservations
* @return Reservations
*/
public function addReservation(\Ipb\AppBundle\Entity\Reservations $reservations)
{
$this->reservations[] = $reservations;
return $this;
}
/**
* Remove reservations
*
* @param \Ipb\AppBundle\Entity\Reservations $reservations
*/
public function removeReservation(\Ipb\AppBundle\Entity\Reservations $reservations)
{
$this->reservations->removeElement($reservations);
}
/**
* Get reservations
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getReservations()
{
return $this->reservations;
}
/**
* Set inviteTemporaire
*
* @param boolean $inviteTemporaire
*
* @return InvitesGroupe
*/
public function setInviteTemporaire($inviteTemporaire)
{
$this->inviteTemporaire = $inviteTemporaire;
return $this;
}
/**
* Get inviteTemporaire
*
* @return boolean
*/
public function getInviteTemporaire()
{
return $this->inviteTemporaire;
}
/**
* Set wine
*
* @param boolean $wine
*
* @return InvitesGroupe
*/
public function setWine($wine)
{
$this->wine = $wine;
return $this;
}
/**
* Get wine
*
* @return boolean
*/
public function getWine()
{
return $this->wine;
}
/**
* Set coffee
*
* @param boolean $coffee
*
* @return InvitesGroupe
*/
public function setCoffee($coffee)
{
$this->coffee = $coffee;
return $this;
}
/**
* Get coffee
*
* @return boolean
*/
public function getCoffee()
{
return $this->coffee;
}
/**
* Set idUser
*
* @param \Ipb\AppBundle\Entity\Users $idUser
*
* @return InvitesGroupe
*/
public function setIdUser(\Ipb\AppBundle\Entity\Users $idUser = null)
{
$this->idUser = $idUser;
return $this;
}
/**
* Get idUser
*
* @return \Ipb\AppBundle\Entity\Users
*/
public function getIdUser()
{
return $this->idUser;
}
}
和预订实体:
<?php
namespace Ipb\AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Reservations
*
* @ORM\Table(name="reservations", indexes={@ORM\Index(name="FK_reservations_id_invite", columns={"id_invite"}), @ORM\Index(name="FK_reservations_id_service", columns={"id_service"}), @ORM\Index(name="FK_reservations_id_restaurant", columns={"id_restaurant"}), @ORM\Index(name="FK_reservations_id_groupe_personnel", columns={"id_groupe_personnel"}), @ORM\Index(name="FK_reservations_id_classe", columns={"id_classe"}), @ORM\Index(name="FK_reservations_id_groupe", columns={"id_groupe"}), @ORM\Index(name="key_date_resa", columns={"date"})})
* @ORM\HasLifecycleCallbacks
* @ORM\Entity(repositoryClass="Ipb\AppBundle\EntityRepository\ReservationsRepository")
*/
class Reservations
{
/**
* @var integer
*
* @ORM\Column(name="id_reservation", type="integer", precision=0, scale=0, nullable=false, unique=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idReservation;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="date", precision=0, scale=0, nullable=true, unique=false)
*/
private $date;
/**
* @var integer
*
* @ORM\Column(name="nb_place", type="integer", precision=0, scale=0, nullable=true, unique=false)
*/
private $nbPlace;
/**
* @var float
*
* @ORM\Column(name="cout_total", type="float", precision=10, scale=0, nullable=true, unique=false)
*/
private $coutTotal;
/**
* @var boolean
*
* @ORM\Column(name="repas_compris", type="boolean", precision=0, scale=0, nullable=true, unique=false)
*/
private $repasCompris;
/**
* @var \Ipb\AppBundle\Entity\GroupesPersonnels
*
* @ORM\ManyToOne(targetEntity="Ipb\AppBundle\Entity\GroupesPersonnels")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_groupe_personnel", referencedColumnName="id_groupe_personnel", nullable=true)
* })
*/
private $idGroupePersonnel;
/**
* @var integer
*
* @ORM\Column(name="incertain", type="integer", precision=0, scale=0, nullable=true, unique=false)
*/
private $incertain;
/**
* @var \Ipb\AppBundle\Entity\Classes
*
* @ORM\ManyToOne(targetEntity="Ipb\AppBundle\Entity\Classes")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_classe", referencedColumnName="id_classe", nullable=true)
* })
*/
private $idClasse;
/**
* @var \Ipb\AppBundle\Entity\Groupes
*
* @ORM\ManyToOne(targetEntity="Ipb\AppBundle\Entity\Groupes")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_groupe", referencedColumnName="id_groupe", nullable=true)
* })
*/
private $idGroupe;
/**
* @var \Ipb\AppBundle\Entity\InvitesGroupe
*
* @ORM\ManyToOne(targetEntity="Ipb\AppBundle\Entity\InvitesGroupe")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_invite", referencedColumnName="id_invite", nullable=true)
* })
*/
private $idInvite;
/**
* @var \Ipb\AppBundle\Entity\Restaurants
*
* @ORM\ManyToOne(targetEntity="Ipb\AppBundle\Entity\Restaurants")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_restaurant", referencedColumnName="id_restaurant", nullable=true)
* })
*/
private $idRestaurant;
/**
* @var \Ipb\AppBundle\Entity\Services
*
* @ORM\ManyToOne(targetEntity="Ipb\AppBundle\Entity\Services")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_service", referencedColumnName="id_service", nullable=true)
* })
*/
private $idService;
/**
*
* @ORM\ManyToOne(targetEntity="Ipb\AppBundle\Entity\InvitesGroupe", inversedBy="reservations", cascade={"persist"})
* @ORM\JoinColumn(name="id_invite", referencedColumnName="id_invite", onDelete="CASCADE")
*/
private $invitesGroupe;
/**
* Get idReservation
*
* @return integer
*/
public function getIdReservation()
{
return $this->idReservation;
}
/**
* Set date
*
* @param \DateTime $date
* @return Reservations
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set nbPlace
*
* @param integer $nbPlace
* @return Reservations
*/
public function setNbPlace($nbPlace)
{
$this->nbPlace = $nbPlace;
return $this;
}
/**
* Get nbPlace
*
* @return integer
*/
public function getNbPlace()
{
return $this->nbPlace;
}
/**
* Set coutTotal
*
* @param float $coutTotal
* @return Reservations
*/
public function setCoutTotal($coutTotal)
{
$this->coutTotal = $coutTotal;
return $this;
}
/**
* Get coutTotal
*
* @return float
*/
public function getCoutTotal()
{
return $this->coutTotal;
}
/**
* Set repasCompris
*
* @param boolean $repasCompris
* @return Reservations
*/
public function setRepasCompris($repasCompris)
{
$this->repasCompris = $repasCompris;
return $this;
}
/**
* Get repasCompris
*
* @return boolean
*/
public function getRepasCompris()
{
return $this->repasCompris;
}
/**
* Set idGroupePersonnel
*
* @param integer $idGroupePersonnel
* @return Reservations
*/
public function setIdGroupePersonnel($idGroupePersonnel)
{
$this->idGroupePersonnel = $idGroupePersonnel;
return $this;
}
/**
* Get idGroupePersonnel
*
* @return integer
*/
public function getIdGroupePersonnel()
{
return $this->idGroupePersonnel;
}
/**
* Set incertain
*
* @param integer $incertain
* @return Reservations
*/
public function setIncertain($incertain)
{
$this->incertain = $incertain;
return $this;
}
/**
* Get incertain
*
* @return integer
*/
public function getIncertain()
{
return $this->incertain;
}
/**
* Set idClasse
*
* @param \Ipb\AppBundle\Entity\Classes $idClasse
* @return Reservations
*/
public function setIdClasse(\Ipb\AppBundle\Entity\Classes $idClasse = null)
{
$this->idClasse = $idClasse;
return $this;
}
/**
* Get idClasse
*
* @return \Ipb\AppBundle\Entity\Classes
*/
public function getIdClasse()
{
return $this->idClasse;
}
/**
* Set idGroupe
*
* @param \Ipb\AppBundle\Entity\Groupes $idGroupe
* @return Reservations
*/
public function setIdGroupe(\Ipb\AppBundle\Entity\Groupes $idGroupe = null)
{
$this->idGroupe = $idGroupe;
return $this;
}
/**
* Get idGroupe
*
* @return \Ipb\AppBundle\Entity\Groupes
*/
public function getIdGroupe()
{
return $this->idGroupe;
}
/**
* Set idInvite
*
* @param \Ipb\AppBundle\Entity\InvitesGroupe $idInvite
* @return Reservations
*/
public function setIdInvite(\Ipb\AppBundle\Entity\InvitesGroupe $idInvite = null)
{
$this->idInvite = $idInvite;
return $this;
}
/**
* Get idInvite
*
* @return \Ipb\AppBundle\Entity\InvitesGroupe
*/
public function getIdInvite()
{
return $this->idInvite;
}
/**
* Set idRestaurant
*
* @param \Ipb\AppBundle\Entity\Restaurants $idRestaurant
* @return Reservations
*/
public function setIdRestaurant(\Ipb\AppBundle\Entity\Restaurants $idRestaurant = null)
{
$this->idRestaurant = $idRestaurant;
return $this;
}
/**
* Get idRestaurant
*
* @return \Ipb\AppBundle\Entity\Restaurants
*/
public function getIdRestaurant()
{
return $this->idRestaurant;
}
/**
* Set idService
*
* @param \Ipb\AppBundle\Entity\Services $idService
* @return Reservations
*/
public function setIdService(\Ipb\AppBundle\Entity\Services $idService = null)
{
$this->idService = $idService;
return $this;
}
/**
* Get idService
*
* @return \Ipb\AppBundle\Entity\Services
*/
public function getIdService()
{
return $this->idService;
}
/**
* Set invitesGroupe
*
* @param \Ipb\AppBundle\Entity\InvitesGroupe $invitesGroupe
* @return Reservations
*/
public function setInvitesGroupes(\Ipb\AppBundle\Entity\InvitesGroupe $invitesGroupe = null)
{
$this->invitesGroupe = $invitesGroupe;
return $this;
}
/**
* Get invitesGroupes
*
* @return \Ipb\AppBundle\Entity\InvitesGroupes
*/
public function getInvitesGroupe()
{
return $this->invitesGroupe;
}
}
控制器:
/**
* Lists all InvitesGroupe entities.
*
* @Route("/", name="invitesgroupe_index")
* @Method("GET")
*/
public function indexAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$day = $request->get('day');
$resto = $request->get('resto');
$user = !$this->get('security.authorization_checker')->isGranted('ROLE_SUPERVISEUR') ? "AND ig.idUser = '".$this->get('security.context')->getToken()->getUser()->getIdUser()."'" : "";
if($day){
$day = \DateTime::createFromFormat('d/m/Y', $day);
$dql = "SELECT ig FROM IpbAppBundle:InvitesGroupe ig
LEFT JOIN IpbAppBundle:Reservations r WITH r.idInvite = ig.idInvite
WHERE ig.inviteVisible = 1
AND ig.inviteTemporaire IS NULL
AND r.date = '".$day->format('Y-m-d')."'
".$user."
ORDER BY ig.idInvite DESC";
}else if($resto && $day){
$day = \DateTime::createFromFormat('d/m/Y', $day);
$dql = "SELECT ig FROM IpbAppBundle:InvitesGroupe ig
LEFT JOIN IpbAppBundle:Reservations r WITH r.idInvite = ig.idInvite
WHERE ig.inviteVisible = 1 AND ig.inviteTemporaire IS NULL
AND r.idRestaurant = ".$resto."
AND r.date = '".$day->format('Y-m-d')."'
".$user."
ORDER BY ig.idInvite DESC";
}else{
$dql = "SELECT ig FROM IpbAppBundle:InvitesGroupe ig
LEFT JOIN IpbAppBundle:Reservations r WITH r.idInvite = ig.idInvite
WHERE ig.inviteVisible = 1 AND ig.inviteTemporaire IS NULL
AND r.date >= CURRENT_TIMESTAMP()
".$user."
ORDER BY ig.idInvite DESC";
}
$query = $em->createQuery($dql);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query, /* query NOT result */
$request->query->getInt('page', 1)/*page number*/,
10/*limit per page*/
);
$deleteForms = array();
if(null !== $request->get('print')){
return $pagination;
}
foreach ($pagination as $page) {
$deleteForms[$page->getIdInvite()] = $this->createDeleteForm($page)->createView();
}
if($day){
$restoTest = array();
$restos = array();
foreach ($pagination as $invite) {
if(!in_array($invite->getIdService()->getIdRestaurant()->getIdRestaurant(), $restoTest)){
$obj = new \stdClass();
$obj->id = $invite->getIdService()->getIdRestaurant()->getIdRestaurant();
$obj->nom = $invite->getIdService()->getIdRestaurant()->getNomRestaurant();
$restos[] = $obj;
$restoTest[] = $invite->getIdService()->getIdRestaurant()->getIdRestaurant();
}
}
}
return $this->render('invitesgroupe/index.html.twig', array(
'invitesGroupes' => $pagination,
'delete_forms' => $deleteForms,
'day' => $day ? $day : null,
'restos' => isset($restos) ? json_encode($restos) : null
));
}