计算实体中的属性



我有一个实体"user",它与实体"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
                    , ));

但是我怎么能把它放在返回中,这样我就可以在我的aftche.html.twig中使用它。因为我想向每个用户显示他拥有的车辆数量。非常感谢这是我的实体Vehicule

<?php
namespace CarPfeBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use OCUserBundleEntityUser;
/**
 * Vehicule
 *
 * @ORMTable()
 * @ORMEntity(repositoryClass="CarPfeBundleEntityVehiculeRepository")
 */
class Vehicule
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     */
    private $id;
    /**
     * @var integer
     *
     * @ORMColumn(name="carte_grise", type="integer", unique=true)
     */
    private $carteGrise;

    /**
     * @var string
     *
     * @ORMColumn(name="modele", type="string", length=255)
     */
    private $modele;
    /**
     * @var string
     *
     * @ORMColumn(name="type", type="string", length=255)
     */
    private $type;
    /**
     * @var string
     *
     * @ORMColumn(name="categorie", type="string", length=255)
     */
    private $categorie;
    /**
     * @var integer
     *
     * @ORMColumn(name="puissance", type="integer")
     *
     */
    private $puissance;
    /**
     * @var integer
     *
     * @ORMColumn(name="nb_place", type="integer")
     */
    private $nbPlace;
    /**
     * @var integer
     *
     * @ORMColumn(name="kilometrage", type="integer")
     */
    private $kilometrage;
    /**
     * @var string
     *
     * @ORMColumn(name="marque", type="string", length=255)
     */
    private $marque;
    /**
     * @var string
     *
     * @ORMColumn(name="carburant", type="string", length=255)
     */
    private $carburant;
    /**
     * @var string
     *
     * @ORMColumn(name="transmission", type="string", length=255)
     */
    private $transmission;
    /**
     * @ORMManyToOne(targetEntity="OCUserBundleEntityUser", inversedBy="vehicule")
     * @ORMJoinColumn(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 OCUserBundleEntityUser $direction
     * @return Vehicule
     */
    public function setDirection(OCUserBundleEntityUser $direction = null)
    {
        $this->direction = $direction;
        return $this;
    }
    /**
     * Get direction
     *
     * @return OCUserBundleEntityUser 
     */
    public function getDirection()
    {
        return $this->direction;
    }
}

这是我的实体用户

 <?php
namespace OCUserBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentSecurityCoreUserUserInterface;
use SymfonyComponentValidatorConstraints as Assert;
use CarPfeBundleEntityVehicule;
/**
 * User
 *
 * @ORMTable()
 * @ORMEntity(repositoryClass="OCUserBundleEntityUserRepository")
 */
class User implements UserInterface
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var string
     *
     * @ORMColumn(name="username", type="string", length=255, unique=true)
     */
    private $username;
    /**
     * @var string
     *
     * @ORMColumn(name="password", type="string", length=255)
     */
    private $password;

    /**
     * @var string
     *
     * @ORMColumn(name="nomDirec", type="string", length=255, unique=true)
     */
    private $nomDirec;
    /**
     * @var string
     *
     * @ORMColumn(name="directeur", type="string", length=255)
     */
    private $directeur;
    /**
     * @var string
     *
     * @ORMColumn(name="adresse", type="string", length=255)
     */
    private $adresse;
    /**
     * @var string
     *
     * @ORMColumn(name="email", type="string", length=255)
     */
    private $email;
    /**
     * @var string
     *
     * @ORMColumn(name="fax", type="integer")
     */
    private $fax;
    /**
     * @var string
     *
     * @ORMColumn(name="tel", type="integer")
     */
    private $tel;
    /**
   * @ORMColumn(name="salt", type="string", length=255)
   */
  private $salt;
    /**
   * @ORMColumn(name="roles", type="array")
   */
  private $roles = array();
 /**
 * @ORMOneToMany(targetEntity="CarPfeBundleEntityVehicule", 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 DoctrineCommonCollectionsArrayCollection();
    }
    /**
     * Add vehicule
     *
     * @param CarPfeBundleEntityVehicule $vehicule
     * @return User
     */
    public function addVehicule(CarPfeBundleEntityVehicule $vehicule)
    {
        $this->vehicule[] = $vehicule;
        return $this;
    }
    /**
     * Remove vehicule
     *
     * @param CarPfeBundleEntityVehicule $vehicule
     */
    public function removeVehicule(CarPfeBundleEntityVehicule $vehicule)
    {
        $this->vehicule->removeElement($vehicule);
    }
    /**
     * Get vehicule
     *
     * @return DoctrineCommonCollectionsCollection 
     */
    public function getVehicule()
    {
        return $this->vehicule;
    }
}

以OOP方式实现这一点的正确方法是定义一个为每个用户返回车辆数量的方法,因此您可以这样做:

在用户类"实体"中定义getNumberOfVehicles函数

public function getNumberOfVehicules()
{
    return $this->vehicule->count();
}

然后在你的trick模板中,你只需调用该函数,即:

{% for user in users %}
    <p>{{user.username}} {{user.getNumberOfVehicules()}}</p>
{% endfor %}

为了获得所需的结果,我会做这样的事情(还没有测试查询,但它应该很有效)

$em = $this->getDoctrine();
$userRepository = $em->getRepository('OCUserBundle:User');
$qb = $userRepository->createQueryBuilder('user')
    ->leftJoin('user.vehicule','vehicule')
    ->addSelect('COUNT(vehicule.id) AS vehicule_count')
    ->groupBy('user.id')
    ->getQuery();
$result = $qb->getResult();

将结果传递给视图

 return $this->render('DemandevehBundle:Demande:affiche.html.twig', 
 array('demandes' => $result ));

在视图中,您可以对结果进行迭代。

$qb = $this->getEntityManager()->createQueryBuilder();
    return $qb->select('u.username as name, count(v.id) as count')
            ->from('OCUserBundle:User', 'u')
            ->leftJoin('u.vehicule', 'v')
            ->groupBy('u.id')
            ->getQuery()
            ->getResult();

在这里,我们得到了一个数组作为结果,每个用户加上没有相应的车辆。只需将结果传递给trick,然后foreach name和count。一切顺利。

相关内容

  • 没有找到相关文章

最新更新