Symfony2 Catchable Fatal Error: Object of class DateTime could not be converted to string

一世执手 提交于 2019-12-12 02:52:32

问题


I have a person (Employee) search that is supposed to return a list of people by name, surname, date of birth and some other parameters. the search works fine for every parameter exempt for the date parameters, like date of birth. my controller code is this:

        $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());
//...

The findByPersonDetails function is this:

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

The error is the following:

 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 

Am I supposed to convert with my own function the date to string? if so Where should I do it to make it reusable? any working example? I thought that Doctrine was able to do this automatically. Thak you in advenced.


回答1:


DateTime objects do not implement a __toString() method, so you will have to explicitly convert them to a string.

Fortunately for you, they do have a format() method that you can utilise to do this for you, for example:

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

See the docs for more information.




回答2:


It works thank you guys @Glavić and @vascowhite, the only thing that I have to do is to make sure that the Date of bird is not empty.

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

I do not really know how professional my code looks like, but it works for me. Thank you again.



来源:https://stackoverflow.com/questions/19134174/symfony2-catchable-fatal-error-object-of-class-datetime-could-not-be-converted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!