在 Doctrine, Symfony2 中调用方法时出错

Error to call a method in Doctrine, Symfony2

我不明白为什么在我的 Symfony2 项目中出现以下错误: 错误:调用非对象上的成员函数 getQueryId()

这是我的代码:

书库:

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

    }
}

getter:

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

控制器:

$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"));

非常感谢您的帮助 ;)

Var 转储结果 wantAuthor($author) 方法并确保您在 return 中获得 Author 对象。我打赌你不会得到一个。

$resultBiblio 是一个带有对象的数组。您在数组上调用方法,这会导致错误。

你可以像这样在foreach中调用这个方法

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