0

I would like to design a query in my repository, but it's a bit complex to do it with the queryBuilder method, so I prefer a complete query.

$queryBuilder = $this->_em->createQueryBuilder(
        "SELECT need_id
        FROM notification_need
        WHERE user_id <> :user
        AND check_date >=
            (SELECT check_date
                FROM notification_need
                WHERE user_id = :user
                ORDER BY check_date DESC
                LIMIT 1)
        AND need_id IN
            (SELECT id
            FROM option_need
            WHERE commande_id IS NULL)
                ")
        ->setParameter('user', $userId);

    return $queryBuilder->getQuery()->getResult();

But an error is generated when executing this request:

[Syntax Error] line 0, col -1: Error: Expected IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | "(" Subselect ")" | CaseExpression, got end of string.

1 Answer 1

3

Query Builder in Doctrine is meant for use with DQL, not plain SQL. If you want to run plain SQL, you need to take the DB connection object from the Entity Manager, and call executeQuery on it:

$conn = $this->_em->getConnection();
$result = $conn->executeQuery(/* your query goes here */);

If you want to use parameters, you can take advantage of prepared statements. In this case, you can work just like with the regular PDO objects:

$stmt = $conn->prepare('SELECT * FROM some_table WHERE id = :id');
$stmt->bindValue(':id', $someId);
$result = $stmt->fetchAll();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.