0
votes

I have an entity "user" that has OneToMany relation with the entity "vehicule". I'm trying to count the number of vehicules for each user. This is my function

  $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
                    , ));

but how could I put it in the return so that i could use it in my affiche.html.twig. because I want to show foreach user the number of vehicules he have . Thanks a lot This is my entity 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;
    }
}

and this is my entity User

 <?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;
    }
}
3
is the desired behavior is to show the user along with the number of vehicles in the same page ? if so mostly need to write a custom query , will be helpful if you can post the entities . - Vamsi Krishna B
I've posted the entities. yes i want to show foreach user the number of vehicules he has all in the same page. - LSoft

3 Answers

0
votes

The correct way to do this in a OOP way , is to define a method that returns the number of vehicules foreach user, so you could to this:

define a getNumberOfVehicules function in your User Class 'Entity'

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

then in your twig template you just simply call that function , i.e:

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

to get the desired result, I would do something like this ( haven't tested the query, but it should pretty much work )

$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();

Pass the result to the view

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

In the view you can iterate over the results.

0
votes
$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();

Here we get an array as result with each user plus no of corresponding vehicles.Just pass result to twig and then foreach name and count.All the best.