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

时间:2015-01-24 13:57:31

标签: symfony doctrine-orm

我不明白为什么我的Symfony2项目出现以下错误: 错误:在非对象

上调用成员函数getQueryId()

以下是我的代码:

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;

    }
}

吸气者:

    /**
     * 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"));

非常感谢你的帮助;)

2 个答案:

答案 0 :(得分:1)

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

您可以在foreach中调用此方法,如

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

答案 1 :(得分:0)

Var转储结果wantAuthor($author)方法并确保您获得Author对象作为回报。我打赌你没有得到一个。