in my repository i have
public function findAllMorosos($date = 'now')
{
$datetime = new \Datetime($date);
$stmt = $this->getEntityManager()
->getConnection()
->prepare(self::sql_morosos);
$stmt->bindValue(':fecha', $datetime, 'datetime');
if ($stmt->execute()) {
return $stmt;
}
return null;
}
my SQL query is
-- SQL
select p.* from inf_pago p
join inf_venta v on v.id = p.venta_id
join inf_cliente c on c.id = v.cliente_id
where p.fecha_pago < ':fecha'
and DATEDIFF(':fecha', p.fecha_pago) >= 30
and p.saldo_por_pagar != 0
when i execute $repository->findAllMorosos() i get empty array (expects 1 row), the query is fine.
when i try:
public function findAllMorosos($fecha = 'now')
{
$datetime = new \Datetime($fecha);
$stmt = $this->getEntityManager()
->getConnection()
->prepare(str_replace(':fecha', $datetime->format('Y-m-d'), self::sql_morosos));
if ($stmt->execute()) {
return $stmt;
}
return null;
}
works fine.
can explain what is wrong with the bindValue method the documentation and more docs not enough
The problem I think is actually in your sql string. Do not wrap the parameter placeholders in quotes. And, I think you need more than one placeholder and binding. Change sql to this:
Then, bind this way:
Note, doctrine uses it’s own statement bindValue implementation which maps the third argument, if it is a string, to a PDO parameter int. Something I did not realize until today 🙂