Symfony2 Catchable致命错误:类DateTime的对象无法转换为字符串

时间:2013-10-02 09:49:26

标签: php string date doctrine symfony-2.3

我有一个人(员工)搜索应该按姓名,姓氏,出生日期和其他一些参数返回人员列表。搜索适用于每个参数免除日期参数,如出生日期。我的控制器代码是这样的:

        $aWorker = new Worker();
    $searchWorkerForm = $this->createFormBuilder($aWorker)
            ->add('omang', 'text', array('required' => false))
            ->add('workerName', 'text', array('required' => false))
            ->add('workerSurname', 'text', array('required' => false))
            ->add('birthDay', 'date', 
                    array('required' => false, 'years' => range(1950, 2020)))
            ->add('dateOfEmployment', 'date', array('required' => false))
            ->add('search', 'submit')
            ->getForm();
    //Handle Request
    if ($request->getMethod() == 'POST')
        $searchWorkerForm->handleRequest($request);

        $aWorkerList = $this->getDoctrine()
             ->getRepository('OsdRetireBundle:Worker')
             ->findByPersonDetails($aWorker->getOmang(),
                     $aWorker->getWorkerName(),
                     $aWorker->getWorkerSurname(),
                     $aWorker->getBirthDay(),
                     $aWorker->getDateOfEmployment());
//...

findByPersonDetails函数是这样的:

public function findByPersonDetails($omang, $WorkerName, 
        $workerSurname, $birthDay, $dateOfEmployment){
   $qqq = $this->getEntityManager()
           ->createQuery('SELECT w FROM OsdRetireBundle:Worker w 
               WHERE w.omang LIKE :omang
               AND w.workerName LIKE :WorkerName
               AND w.workerSurname LIKE :workerSurname
               AND w.birthDay LIKE :birthDay
               AND w.dateOfEmployment LIKE :dateOfEmployment')
           ->setParameter('omang', '%'.$omang.'%')
           ->setParameter('WorkerName', '%'.$WorkerName.'%')
           ->setParameter('workerSurname', '%'.$workerSurname.'%')
           ->setParameter('birthDay', '%'.$birthDay.'%')
           ->setParameter('dateOfEmployment', '%'.$dateOfEmployment.'%')
           ->getResult();
   return $qqq;
}

错误如下:

 Catchable Fatal Error: Object of class DateTime could not be converted to string in /var/www/Org/src/Osd/RetireBundle/Entity/WorkerRepository.php line 27
500 Internal Server Error - ContextErrorException 

我是否应该使用自己的函数将日期转换为字符串?如果是的话我应该在哪里做以使其可重复使用?任何工作的例子? 我以为Doctrine能够自动完成这项工作。 告诉你你的意思。

2 个答案:

答案 0 :(得分:5)

DateTime对象没有实现__toString()方法,因此您必须将它们显式转换为字符串。

幸运的是,他们确实有format()方法,您可以利用它来为您执行此操作,例如:

->setParameter('birthDay', '%' . $birthDay->format('Y-m-d') . '%')

有关详细信息,请参阅docs

答案 1 :(得分:0)

感谢你们@Glavić和@vascowhite,我唯一需要做的就是确保鸟的日期不是空的。

    public function findByPersonDetails($omang, $WorkerName, 
        $workerSurname, $birthDay, $dateOfEmployment){
    $createQuery = 'SELECT w FROM OsdRetireBundle:Worker w 
               WHERE w.omang LIKE :omang
               AND w.workerName LIKE :WorkerName
               AND w.workerSurname LIKE :workerSurname';
     if ($birthDay != '')
        $createQuery .= ' AND w.birthDay LIKE :birthDay';

     if ($dateOfEmployment != '')
        $createQuery .= 'AND w.dateOfEmployment LIKE :dateOfEmployment';

    $qqq = $this->getEntityManager()
           ->createQuery($createQuery)
           ->setParameter('omang', '%'.$omang.'%')
           ->setParameter('WorkerName', '%'.$WorkerName.'%')
           ->setParameter('workerSurname', '%'.$workerSurname.'%');
    if ($birthDay != '') $qqq = $qqq->setParameter('birthDay', '%'.$birthDay->format('Y-m-d').'%');
    if ($dateOfEmployment != '') $qqq = $qqq->setParameter('dateOfEmployment'
            , '%'.$dateOfEmployment->format('Y-m-d').'%');
    $qqq = $qqq->getResult();
   return $qqq;
}

我真的不知道我的代码看起来有多专业,但它对我有用。 再次感谢你。