单引号和双引号如何转义函数?

问题描述:

我试图使用doctrine日期格式功能,orded在m-d今天m-d比较日期不知道如何处理逃避我addWhere,这里是一个片段:单引号和双引号如何转义函数?

->andWhere('date("m-d", strtotime('r.dateOfDeath')) = :now') 
->setParameter('now',\date("m-d", time())) 
+1

你尝试或者通过\(反斜杠)转义他们或只是改为一种类型的报价? – kero

+0

是的,我做了,我认为问题从日期功能开始,我尝试使用(\)转义它并且它没有解决问题。 – Amr

如果你的目标是要通过dateOfDeath以特定的月份/日期组合进行搜索,您可能会发现Doctrine缺乏。

在MySQL中,你通常会做WHERE MONTH(r.dateOfDeath) = MONTH(NOW()) AND DAY(r.dateOfDeath) = DAY(NOW()) 为了让自己更容易,你可以使用本机MySQL查询, Execute raw SQL using Doctrine 2

在学说中,DAY,NOW和MONTH函数都丢失了。现在你可以用参数解决这个问题。日和月功能你需要通过像束来实现:https://github.com/beberlei/DoctrineExtensions

另一种可能的解决方案可能是简单地让dateOfDeath一个字符串,并做了->andWhere("r.dateOfDeath LIKE '%:md')->setParameter('md', date('m-d'))

+0

我的问题是关于'm-d'格式与'm-d'格式的现在日期的比较记录日期,使用DateTime将使用时间。 – Amr

+0

我更新了答案,以更好地反映您的问题。 –

+0

非常感谢@Rein我需要为您的答案投票,但我需要15点声望分数:) – Amr

我使用学说的扩展解决问题[见]:https://github.com/uvd/Doctrine/tree/master/UVd/DoctrineFunction “UVD主义”

config.yml

orm: 
    default_entity_manager: default 
    entity_managers: 
     default: 
      connection: default 
      dql: 
       datetime_functions: 
        DATE_FORMAT: AppBundle\Extension\Doctrine\DateFormat 

使用

寄存器功能

protected function registerDateFunctions() 
{ 
    $emConfig = $this->getEntityManager()->getConfiguration(); 
    $emConfig->addCustomStringFunction('DATE_FORMAT','AppBundle\Extensions\Doctrine\DateFormat'); 
    } 

创建查询生成器

public function getAllRecords() 
{ 
    $this->registerDateFunctions(); 
    $qb = $this->createQueryBuilder('r') 
     ->select('r') 
     ->addSelect('d') 
     ->addSelect('b.country') 
     ->leftJoin('r.religion', 're') 
     ->leftJoin('r.country_of_birth', 'b') 
     ->leftJoin('r.country_of_death', 'd') 
     ->where('r.status = :status') 
     ->setParameter(':status', 1) 
     ->andWhere("DATE_FORMAT(r.dateOfDeath,'%m-%d') = :now") 
     ->setParameter(':now',\date("m-d", time())) 
     ->getQuery(); 
    return $qb->getArrayResult(); 

} 

信用: “http://www.uvd.co.uk/blog/using-mysqls-date_format-in-doctrine-2-0/