简体   繁体   中英

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM