Sunny
Sunny

Reputation: 125

Doctrine queryBuilder setParamater

I'm having a problem using Parameters in the Doctrine queryBuilder.

Here's my code:

$queryBuilder
    ->select('id', 'value')
    ->from('test')
    ->where('id = :id')
    ->setParameter('id', '1', 'integer')
;

This creates:

SELECT id, value FROM test WHERE id = :id

However the setParameter is not applied when I use

$stmt = $conn->query($queryBuilder);

Getting this Error: "Fatal error: Uncaught exception 'Doctrine\DBAL\Driver\Mysqli\MysqliException' with message 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':id'..."

What am I missing?

Using this works, but I would prefer to integrate the parameters directly in the queryBuilder:

$stmt = $conn->prepare($queryBuilder);
$stmt->bindValue("id", "1", "integer");
$stmt->execute();

Any hints would be great.

Upvotes: 2

Views: 170

Answers (1)

tom
tom

Reputation: 8359

It's possible to directly execute the querybuilder, this will attach the paramaters correctly.

Example:

$queryBuilder = $conn->createQueryBuilder();
$queryBuilder
    ->select('id', 'value')
    ->from('test')
    ->where('id = :id')
    ->setParameter('id', '1', 'integer');

$queryBuilder->execute();

Upvotes: 1

Related Questions