我有一個與實體「vehicule」具有OneToMany關係的實體「用戶」。我正在計算每個用戶的車輛數量。這是我的功能統計實體中的屬性
$emm = $this->getDoctrine();
$directions = $emm->getRepository('OCUserBundle:User')->findAll();
foreach($directions as $direction) {
$direction->getVehicule()->count();
}
return $this->render('DemandevehBundle:Demande:affiche.html.twig', array('demandes' => $demandes
,));
但我怎麼能把它放在返回,以便我可以在我的affiche.html.twig中使用它。因爲我想向每位用戶展示他擁有的車輛數量。非常感謝 這是我的實體VEHICULE
<?php
namespace Car\PfeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use OC\UserBundle\Entity\User;
/**
* Vehicule
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Car\PfeBundle\Entity\VehiculeRepository")
*/
class Vehicule
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="carte_grise", type="integer", unique=true)
*/
private $carteGrise;
/**
* @var string
*
* @ORM\Column(name="modele", type="string", length=255)
*/
private $modele;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=255)
*/
private $type;
/**
* @var string
*
* @ORM\Column(name="categorie", type="string", length=255)
*/
private $categorie;
/**
* @var integer
*
* @ORM\Column(name="puissance", type="integer")
*
*/
private $puissance;
/**
* @var integer
*
* @ORM\Column(name="nb_place", type="integer")
*/
private $nbPlace;
/**
* @var integer
*
* @ORM\Column(name="kilometrage", type="integer")
*/
private $kilometrage;
/**
* @var string
*
* @ORM\Column(name="marque", type="string", length=255)
*/
private $marque;
/**
* @var string
*
* @ORM\Column(name="carburant", type="string", length=255)
*/
private $carburant;
/**
* @var string
*
* @ORM\Column(name="transmission", type="string", length=255)
*/
private $transmission;
/**
* @ORM\ManyToOne(targetEntity="OC\UserBundle\Entity\User", inversedBy="vehicule")
* @ORM\JoinColumn(name="User_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $direction;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set Id
*
* @param integer $carteGrise
* @return Vehicule
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Set carteGrise
*
* @param integer $carteGrise
* @return Vehicule
*/
public function setCarteGrise($carteGrise)
{
$this->carteGrise = $carteGrise;
return $this;
}
/**
* Get carteGrise
*
* @return integer
*/
public function getCarteGrise()
{
return $this->carteGrise;
}
/**
* Set modele
*
* @param string $modele
* @return Vehicule
*/
public function setModele($modele)
{
$this->modele = $modele;
return $this;
}
/**
* Get modele
*
* @return string
*/
public function getModele()
{
return $this->modele;
}
/**
* Set categorie
*
* @param string $categorie
* @return Vehicule
*/
public function setCategorie($categorie)
{
$this->categorie = $categorie;
return $this;
}
/**
* Get categorie
*
* @return string
*/
public function getCategorie()
{
return $this->categorie;
}
/**
* Set puissance
*
* @param integer $puissance
* @return Vehicule
*/
public function setPuissance($puissance)
{
$this->puissance = $puissance;
return $this;
}
/**
* Get puissance
*
* @return integer
*/
public function getPuissance()
{
return $this->puissance;
}
/**
* Set nbPlace
*
* @param integer $nbPlace
* @return Vehicule
*/
public function setNbPlace($nbPlace)
{
$this->nbPlace = $nbPlace;
return $this;
}
/**
* Get nbPlace
*
* @return integer
*/
public function getNbPlace()
{
return $this->nbPlace;
}
/**
* Set kilometrage
*
* @param integer $kilometrage
* @return Vehicule
*/
public function setKilometrage($kilometrage)
{
$this->kilometrage = $kilometrage;
return $this;
}
/**
* Get kilometrage
*
* @return integer
*/
public function getKilometrage()
{
return $this->kilometrage;
}
/**
* Set marque
*
* @param string $marque
* @return Vehicule
*/
public function setMarque($marque)
{
$this->marque = $marque;
return $this;
}
/**
* Get marque
*
* @return string
*/
public function getMarque()
{
return $this->marque;
}
/**
* Set carburant
*
* @param string $carburant
* @return Vehicule
*/
public function setCarburant($carburant)
{
$this->carburant = $carburant;
return $this;
}
/**
* Get carburant
*
* @return string
*/
public function getCarburant()
{
return $this->carburant;
}
/**
* Set transmission
*
* @param string $transmission
* @return Vehicule
*/
public function setTransmission($transmission)
{
$this->transmission = $transmission;
return $this;
}
/**
* Get transmission
*
* @return string
*/
public function getTransmission()
{
return $this->transmission;
}
public function __toString()
{
return (string)$this->id;
}
/**
* Set type
*
* @param string $type
* @return Vehicule
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Set direction
*
* @param \OC\UserBundle\Entity\User $direction
* @return Vehicule
*/
public function setDirection(\OC\UserBundle\Entity\User $direction = null)
{
$this->direction = $direction;
return $this;
}
/**
* Get direction
*
* @return \OC\UserBundle\Entity\User
*/
public function getDirection()
{
return $this->direction;
}
}
,這是我的實體用戶
<?php
namespace OC\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Car\PfeBundle\Entity\Vehicule;
/**
* User
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="OC\UserBundle\Entity\UserRepository")
*/
class User implements UserInterface
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=255, unique=true)
*/
private $username;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* @var string
*
* @ORM\Column(name="nomDirec", type="string", length=255, unique=true)
*/
private $nomDirec;
/**
* @var string
*
* @ORM\Column(name="directeur", type="string", length=255)
*/
private $directeur;
/**
* @var string
*
* @ORM\Column(name="adresse", type="string", length=255)
*/
private $adresse;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255)
*/
private $email;
/**
* @var string
*
* @ORM\Column(name="fax", type="integer")
*/
private $fax;
/**
* @var string
*
* @ORM\Column(name="tel", type="integer")
*/
private $tel;
/**
* @ORM\Column(name="salt", type="string", length=255)
*/
private $salt;
/**
* @ORM\Column(name="roles", type="array")
*/
private $roles = array();
/**
* @ORM\OneToMany(targetEntity="Car\PfeBundle\Entity\Vehicule", mappedBy="direction", cascade={"remove", "persist"})
*/
protected $vehicule;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
* @return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set password
*
* @param string $password
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set roles
*
* @param array $roles
* @return User
*/
public function setRoles($roles)
{
$this->roles = $roles;
return $this;
}
/**
* Get roles
*
* @return array
*/
public function getRoles()
{
return $this->roles;
}
public function eraseCredentials()
{
}
/**
* Set nomDirec
*
* @param string $nomDirec
* @return User
*/
public function setNomDirec($nomDirec)
{
$this->nomDirec = $nomDirec;
return $this;
}
/**
* Get nomDirec
*
* @return string
*/
public function getNomDirec()
{
return $this->nomDirec;
}
/**
* Set directeur
*
* @param string $directeur
* @return User
*/
public function setDirecteur($directeur)
{
$this->directeur = $directeur;
return $this;
}
/**
* Get directeur
*
* @return string
*/
public function getDirecteur()
{
return $this->directeur;
}
/**
* Set adresse
*
* @param string $adresse
* @return User
*/
public function setAdresse($adresse)
{
$this->adresse = $adresse;
return $this;
}
/**
* Get adresse
*
* @return string
*/
public function getAdresse()
{
return $this->adresse;
}
/**
* Set email
*
* @param string $email
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set fax
*
* @param \integer $fax
* @return User
*/
public function setFax($fax)
{
$this->fax = $fax;
return $this;
}
/**
* Get fax
*
* @return \integer
*/
public function getFax()
{
return $this->fax;
}
/**
* Set tel
*
* @param integer $tel
* @return User
*/
public function setTel($tel)
{
$this->tel = $tel;
return $this;
}
/**
* Get tel
*
* @return integer
*/
public function getTel()
{
return $this->tel;
}
/**
* Set salt
*
* @param string $salt
* @return User
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* Get salt
*
* @return string
*/
public function getSalt()
{
return $this->salt;
}
public function __toString()
{
return strval($this->getId());
}
/**
* Constructor
*/
public function __construct()
{
$this->vehicule = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add vehicule
*
* @param \Car\PfeBundle\Entity\Vehicule $vehicule
* @return User
*/
public function addVehicule(\Car\PfeBundle\Entity\Vehicule $vehicule)
{
$this->vehicule[] = $vehicule;
return $this;
}
/**
* Remove vehicule
*
* @param \Car\PfeBundle\Entity\Vehicule $vehicule
*/
public function removeVehicule(\Car\PfeBundle\Entity\Vehicule $vehicule)
{
$this->vehicule->removeElement($vehicule);
}
/**
* Get vehicule
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getVehicule()
{
return $this->vehicule;
}
}
是希望的行爲是顯示用戶以及在同一頁面的車輛數量?如果大多數情況下需要編寫自定義查詢,則可以發佈實體。 – Vamsi
我已經發布了實體。是的,我想向用戶展示他在同一頁面中所有車輛的數量。 –