0

I build a query which does return results since i var dump the objects, however, when I loop through the results, I get an error during the loop stage.

I have tried to looking on online as to why I am getting the error, including the php documentation.

Here is my querybuilder.

/** @var QueryBuilder $qb */
    $qb = $repository->createQueryBuilder('i');
    $qb->select('j.fund_code', 'j.amount', 'i.source_code', 
 'i.keyword', 'i.created_at', 'i.status', 'g.transaction_id')
        ->join('i.gateway_response', 'g')
        ->join('i.items', 'j')
        ->Where("i.status = :status")
        ->andWhere($qb->expr()->between('i.created_at', 
 ':starts_at', ':ends_at'))
        ->setParameter('status', 2)
        ->setParameter('starts_at', $startsAt, 
 \Doctrine\DBAL\Types\Type::DATETIME)
        ->setParameter('ends_at', $endsAt, 
\Doctrine\DBAL\Types\Type::DATETIME)
            ;

    $query = $qb->getQuery();
    $results = $query->getResult();
    $resultCount = count($results);
        var_dump($results);

Here is my If fucntion with a foreach loop.

 if($resultCount > 0) {
        foreach($results as $holdTran) {

            $status = $holdTran->getStatus();


        }

        $output->writeln($resultCount . ': Transactions on Hold');
        $output->writeln($status);


        $mailer = $this->getContainer()->get('mailer');
             $message = new \Swift_Message();
             $message
                ->setSubject('Daily Giving Report')
                ->setFrom('chris2kus31@gmail.com')
                ->setTo('cmoreno@kcm.org')
                ->setContentType("text/html")
                ->setBody($this->getContainer()->get('templating')- 
>render('@Giving/Default/givingReport.html.twig',
                                [
                                    'resultCount' => $resultCount,
                                        'status'=> $status
                                ])

                    );

Here is the error I am getting in return

Fatal error: Call to a member function getStatus() on array

What I am trying to do is do a query, get the results that will pass the objects to a twig template

varDump Resutl

array(1) {
  [0] =>
  array(7) {
   'fund_code' =>
    string(4) "K100"
    'amount' =>
   double(20)
    'source_code' =>
    NULL
    'keyword' =>
   NULL
   'created_at' =>
  class DateTime#675 (3) {
    public $date =>
    string(26) "2019-03-26 08:03:20.000000"
    public $timezone_type =>
    int(3)
    public $timezone =>
   string(15) "America/Chicago"
     }
     'status' =>
     int(2)
     'transaction_id' =>
     string(12) "000035772641"
   }
 }
  • Can you show us the var_dump result? – Elanochecer Mar 28 '19 at 14:59
  • `$holdTran` is an array, not an object, so `getStatus()` won't work. `var_dump($holdTran);` to see exactly what it holds and how to get the data you want. – aynber Mar 28 '19 at 14:59
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – aynber Mar 28 '19 at 15:00
  • @elanochecer, i added the vardump result –  Mar 28 '19 at 15:03
  • @Jesus2kus just replace $status = $holdTran->getStatus(); with $status = $holdTran['status']; – Elanochecer Mar 28 '19 at 15:05
  • @elonochecer oh man, I cant believer i missed that! Thank you so much. –  Mar 28 '19 at 15:08
  • @Elanochecer if there is multiple results, it only returns the last one. should i leave the [] empty or what do you recommend? –  Mar 28 '19 at 15:43
  • @Jesus2kus ot depends on what you want to do with it. 1 email per result? 1 email with a list of results? – Elanochecer Mar 28 '19 at 17:18

0 Answers0