2012-01-10 84 views
0

在我的仓库,我有声明中Doctrine2

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; 
} 

我的SQL查询是

-- 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 

当我执行$repository->findAllMorosos()我得到空数组(预计1行),查询是罚款。

当我尝试:

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; 
} 

工作正常。

能解释一下什么是错的bindValue方法documentationmore docs不够

回答

1

我认为实际上是在你的SQL字符串的问题。不要将参数占位符包装在引号中。而且,我认为你需要多个占位符和绑定。更改SQL这样的:

where p.fecha_pago < :fecha1 
and DATEDIFF(:fecha2, p.fecha_pago) >= 30 

然后,结合这样:

$stmt->bindValue(':fecha1', $datetime, 'datetime'); 
$stmt->bindValue(':fecha2', $datetime, 'datetime'); 

注意,学说使用它自己的说法bindValue实现它映射的第三个参数,如果它是一个字符串,在PDO参数INT。一些我直到今天才意识到的东西:)

+0

你怎么看[this](http://www.doctrine-project.org/docs/dbal/2.2/en/reference/data-retrieval-and-manipulation的.html#doctrinedbaltypes转换)? – rkmax 2012-01-10 17:21:39

+0

是的问题是查询中的引号 – rkmax 2012-01-10 17:57:37