Orm count 1: n doctrine

I am using the orm doctrine with the admingenerator module from symfony2, and I can’t do a quantity count involving two tables. I would really appreciate any thoughts on this. Thank you very much in advance!

This is a querybuilder expression :

class ListController extends BaseListController
{
protected function getQuery()
    {
        $query = $this->getDoctrine()
                    ->getEntityManager()
                    ->createQueryBuilder()
                    ->select('q, count(f.fbid) AS no')
                    ->from('Shlomi\UsersBundle\Entity\users', 'q')
                    ->leftJoin('q.fbid' , 'f')
                    ->groupBy('q.fbid');


        $this->processSort($query);
        $this->processFilters($query);
                $this->processScopes($query);

        return $query->getQuery();
    }
}

Two objects that I use:

class Users
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var bigint $fbid
     *
     * @ORM\OneToMany(targetEntity="Friendships", mappedBy ="fbid")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="fbid", referencedColumnName="fbid")
     * })
     */
    private $fbid;
   ....

AND

class Friendships
{
    /**
     * @var integer $Id
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $Id;

   /**
     * @var Users
     *
     * @ORM\ManyToOne(targetEntity="Users", inversedBy="fbid")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="fbid", referencedColumnName="fbid")
     * })
     */
    private $fbid;
....

While the association annotation in Friendships was automatically generated by the doctrine: generate-entities (containing a link to the fbid foreign key from Users.fbid), the annotation in Users (1: n) was manually inserted as far as I know

I tried to edit the query builder and annotation again and again, but in the end I get the following:

( "Catchable Fatal Error: Object of Doctrine\ORM\PersistentCollection C:\xampp\htdocs\symfony2\app\cache\dev\twig\ea\85\2b678090e942db52cc01e3950dbc.php line 225" ) Admingenerated/ShlomiUsersBundle/Resources/views/UsersList/index.html.twig 92.

,

+3
1

Try:

    $qb = $this->getDoctrine()
               ->getEntityManager()
               ->createQueryBuilder();
    $query = $qb->select('q', $qb->expr()->count('f.fbid'))
                ->from('Shlomi\UsersBundle\Entity\users', 'q')
                ->leftJoin('q.fbid' , 'f')
                ->groupBy('q.fbid');

querybuilder ($ qb), , $qb ( expr() count())

+1

All Articles