0

I'm trying to convert this MySql query

SELECT appcs_training.name, appcs_training.id FROM appcs_training WHERE appcs_training.id NOT IN (SELECT training_id FROM appcs_user_purchased_trainings WHERE user_id = 54)

into a queryBuilder but I am getting the following error over and over again... I would appreciate your help to solve this.

[Semantical Error] line 0, col 57 near 'training FROM': Error: Invalid PathExpression. Must be a StateFieldPathExpression.

And this is my code:

    public function findNonPurchasedTrainingsByUserId($userId)
{
    $expr = $this->em->getExpressionBuilder();
    return $this->em->createQueryBuilder()
        ->select('t')
        ->from('App:Training', 't')
        ->where($expr->notIn(
            't.id',
            $this->em->createQueryBuilder()
                ->select('ut.training')
                ->from('App:UserTraining', 'ut')
                ->where('ut.user = :userId')
                ->setParameter('userId', $userId)
                ->getDQL()
        ))
        ->getQuery()
        ->getResult();
}

1 Answers1

0

Finally, setParameter wasn't valid. This is the only way I made it work

public function findNonPurchasedTrainingsByUserId(int $userId)
{
    $expr = $this->getEntityManager()->getExpressionBuilder();

    $subQuery = $this->em->createQueryBuilder()
        ->select('tr.id')
        ->from('App:UserTraining', 'ut')
        ->leftJoin('App:Training', 'tr', Join::WITH, $expr->eq('ut.training', 'tr.id'))
        ->where($expr->eq('ut.user', $userId))
        ->getDQL()
    ;

    $query = $this->em->createQueryBuilder();
    $query->Select('t')
        ->from('App:Training', 't')
        ->where($query->expr()->notIn('t.id', $subQuery))
        ->andWhere($query->expr()->eq('t.isActive', true))
        ->setMaxResults(8)
    ;
    return  $query->getQuery()->getResult();
}