In Doctrine
, is it possible to bind the name of a parameter
(contrary to binding the value of a parameter)?
Why I need it
There is a table having 7 boolean columns, one for each day of the week: monday
, tuesday
, etc. (these correspond to the structure of the calendar
entity defined by GTFS, https://gtfs.org/schedule/reference/#calendartxt).
Given a day of the week (say, monday
), I want to get all rows, which are available on Mondays, i.e.:
$statement = $this
->getEntityManager()
->getConnection()
->prepare('
SELECT id
FROM calendar
WHERE monday = 1
');
Generally, I want to be able to supply the name of a day in that query, which I can do simply by:
->prepare("
SELECT id
FROM calendar
WHERE $dayName = 1
");
I wonder whether it's possible to use the parameter binding for the names of parameters, i.e. something like
$statement = $this
->getEntityManager()
->getConnection()
->prepare('
SELECT id
FROM calendar
WHERE :dayName = 1
');
$statement->bindValue('dayName', $dayName);
which does not work, see below.
What I tried
#1
WHERE :dayName = 1
which translates to the following SQL query:
SELECT calendar.id
FROM calendar
WHERE 'monday' = 1
and, because that condition is never true, returns an empty set, []
.
#2
WHERE `:dayName` = 1
SELECT calendar.id
FROM calendar
WHERE `'monday'` = 1
Column not found: 1054 Unknown column ''monday'' in 'where clause'
#3
WHERE ":dayName" = 1
# no query
Invalid parameter number: number of bound variables does not match number of tokens