viher
viher

Reputation: 114

Get username with queryBuilder join using FOSUserBundle

I use FOSUserBundle, and I need to get the user names who wrote comments on my post.

I use function createQuery to get the post comments, and add this join to trying to get the names:

<?php

namespace CommentsBundle\Entity;

use FOS\UserBundle\Entity\User;

/**
 * CommentsRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class CommentsRepository extends \Doctrine\ORM\EntityRepository
{
    public function getPostComments($planId)
    {
        $arrPlanComments = array();

        $query = $this->getEntityManager()->createQuery(

            'SELECT 
             c.id, 
             c.fkUser, 
             c.fkReply, 
             c.comment, 
             c.createdAt, 
             c.likes, 
             c.unlikes 
             FROM CommentsBundle:Comments c 
             INNER JOIN UserBundle:User u 
             WITH (c.fkUser = u.id) 
             WHERE 
             c.fkPlan = :planId 
             ORDER BY c.createdAt DESC'

        )->setParameter('planId', $planId);

        try
        {
            $arrPlanComments = $query->getArrayResult();
        }
        catch(\Doctrine\ORM\NoResultException $ex)
        {
            echo $ex->getMessage();
        }

        return $arrPlanComments;
    }

}

I have extended the FOSUserBundle to my custom bundle named 'UserBundle', and it works fine, but I don't know how to add the relationship with this entity.

I am having this error when i add the join relationship:

[Semantical Error] line 0, col 164 near 'UserBundle:User': Error: Class 'UserBundle\Entity\User' is not defined.

What is wrong?

Upvotes: 2

Views: 256

Answers (1)

michaJlS
michaJlS

Reputation: 2500

According to the documentation, you have to create own class for User entity, which will extend one provided by FOSUser. It seems like you haven't done that. That's what your error message probably indicates.

Upvotes: 2

Related Questions