0
votes

I am trying to do a query in Symfony 3 to select the Commentaries of a Wish (Object Wishcom) with the contributionType included (Object ContributionType) thanks to a JOIN.

When running the webpage I get a:

Error: Method Doctrine\ORM\Query\Expr\Func::__toString() must not throw an >exception, caught Symfony\Component\Debug\Exception\ContextErrorException: >Catchable Fatal Error: Object of class DateTime could not be converted to >string

In the forums I could get that the datetime has to be converted with format. The thing is that I am not manipulating directly the date with my query although I know there is one DateTime attribute in my Wishcom object.

Should I select specifically the date and format it ? In that case were should it be done ? Or does the error come from something else ?

It seems the error comes from the where statement and the function ToString from Expr not being able to convert the date. I don't know what I should do.

$queryBuilder->where($queryBuilder->expr()->in('w.wish', $wish));

Here is my call in the controller:

$arraywishcom=$em->getRepository(Wishcom::class)->getWishcomWithContributionType($wish);

And my repository:

<?php

namespace Shaker\JRQBundle\Repository;

use Shaker\JRQBundle\Entity\Wish;
/**
 * WishcomRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class WishcomRepository extends \Doctrine\ORM\EntityRepository
{

    public function getWishcomWithContributionType(Wish $wish) {

        $queryBuilder = $this
    ->createQueryBuilder('w')
    ->leftJoin('w.contributiontype', 'contributiontype')
    ->addSelect('contributiontype')
    ;

    $queryBuilder->where($queryBuilder->expr()->in('w.wish', $wish));

    return $queryBuilder
    ->getQuery()
    ->getResult()
    ;
    }
}
1
Are you using twig? If so pass that DateTime value to twigs "date" (twig.symfony.com/doc/2.x/filters/date.html) - Alex.Barylski
I am. I had writen it like that: <p> {{wishcom.ContributionType}} : {{wishcom.content}} de {{wishcom.user}} le {{wishcom.addeddate|date("d/m/Y")}} </p>. But according to the error log the fatal error comes from the Controller and the getWishcomWithContributionType() method where the query is executed. - Shaker81

1 Answers

0
votes

I have found a different way of doing the query and it works. Instead of the where I had with expr(), which was the problem, I did the following:

$queryBuilder->where('w.wish = :wish')->setParameter('wish', $wish);

But I still do not know why I got the error in the first place.