简体   繁体   中英

Error to call a method in Doctrine, Symfony2

I don't understand why I get the following error in my Symfony2 project: Error: Call to a member function getQueryId() on a non-object

Here are my codes:

Bibliorepository:

<?php

namespace Xxxx\XxxxxBundle\Repository;

use Doctrine\ORM\EntityRepository;

/**
 * BiblioRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class BiblioRepository extends EntityRepository
{
    public function wantAuthor($author)
      {

        $query = $this->_em->createQuery('SELECT b FROM XxxxBundle:Biblio b WHERE b.author = :author');
        $query->setParameter('author', $author);

        $result_author = $query->getResult();

      return $result_author;

    }
}

The getter:

    /**
     * Get queryId
     *
     * @return integer
     */
    public function getQueryId()
    {
        return $this->queryId;
    }
}

And the controller:

$author = $this->getUser()->getId();

        $repository = $this

    ->getDoctrine()

    ->getManager()

    ->getRepository('XxxxBundle:Biblio');

  $resultBiblio = $repository->wantAuthor($author);
  $resultBiblio->getQueryId();
  foreach ($resultBiblio as $id_query) {

    $repository = $this

    ->getDoctrine()

    ->getManager()

    ->getRepository('XxXxBundle:Query');

    $resultQuery = $repository->wantQuery($id_query);
    $titles = $resultQuery->getQuery();

    }




        return $this->redirect($this->generateUrl("fos_user_profile_show"));

Thank you very much for your help ;)

$resultBiblio is an array with object. You invoke a method on array and this causes error.

You can invoke this method in foreach like

  foreach ($resultBilbo as $singleResult){
   $singleResult->getQueryId();
  }

Var dump results wantAuthor($author) method and make sure you're getting the Author object in return. I bet you're not getting one.

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