1

I'm making a query using the QueryBuilder object from Doctrine. In that query I have a join which I don't know why, gives me an error saying that the alias (fe) for one of the tables (F_Expenses) is not defined. In SQL, my query would looks like this:

select * from f_expenses
join users_f_expenses on f_expenses.id=users_f_expenses.id_form
where login="Jack1234";

This being said, this is the function which contains my QueryBuilder object:

public function getFormsAction($login){

    $em = $this->getDoctrine()->getEntityManager();
    $qb = $em->createQueryBuilder()
             ->select('fe')
             ->from('intranetBundle:Entity\F_Expenses', 'fe')
             ->innerJoin('fe', 'intranetBundle:Entity\Users_F_Expenses', 'ufe', 'fe.id = ufe.idForm')
             ->where('ufe.login = :login')
             ->setParameter('login', $login)
             ->getQuery();


    $formsOfTheUser = $qb->getArrayResult();

    $serializer = SerializerBuilder::create()->build();
    $serializer->serialize($formsOfTheUser, 'json');

    return $formsOfTheUser;
}

Note that yes, the field called id_form in the database, is named as idForm in the Entity. This is exactly the error:

[Semantical Error] line 0, col 62 near 'fe intranetBundle:Entity\Users_F_Expenses': Error: Class 'fe' is not defined.

I've just followed the API, first argument of the join is used to specify the alias of the FROM, so fe. Second and third parameters are, respectively, the join-table and its alias (so intranetBundle:Entity\Users_F_Expenses and ufe), and finally the fourth is the on clause (fe.id = ufe.idForm).

I've tried to delete the join line and make a normal select from the first table and it worked, so I'm pretty sure that the error has something to do with the join clause.

Drumnbass
  • 867
  • 1
  • 14
  • 35
  • 1
    I don't think query builder can do joins unless you explicitly add joins in your entities/schema files. You can still do joins of course but you will have to do them in native SQL or possibly use DQL. But you might want to consider adding you relations to your entity files. See: http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html – Oli May 28 '16 at 16:09

0 Answers0