Symfony 使用findBy方法

Symfony 使用findBy方法,symfony,doctrine-orm,Symfony,Doctrine Orm,我正在从事博客项目,其中有许多来自不同作者的帖子,我想创建一个简单的过滤器,只返回给定作者的帖子: 这是我的控制器,从用户处获取数据: /** * @Route("/author/{author}", name="post_author") * @Template() */ public function findByAuthorAction($author) { $criteria = /*this is what i need*

我正在从事博客项目,其中有许多来自不同作者的帖子,我想创建一个简单的过滤器,只返回给定作者的帖子:

这是我的控制器,从用户处获取数据:

    /**
     * @Route("/author/{author}", name="post_author")
     * @Template()
     */
    public function findByAuthorAction($author)
    {

    $criteria = /*this is what i need*/
    $posts=$this->get('cvut_fit_biwt1_blog')->findPostBy($criteria);

    return array(
        'posts'=>$posts
    );
}
这就是findPostBy的外观:

public function findPostBy(array $criteria)
    {
        return $this->postRepository->findBy($criteria);
    }
最后,在实体报告理论中实施findBy:

 public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
    {
        $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);

        return $persister->loadAll($criteria, $orderBy, $limit, $offset);
    }

您能告诉我如何在我的控件中构建criteria数组,以便它能够过滤我的帖子(如果可能的话)?

假设
{author}
参数是作者的URI名称或其他什么(这纯粹是推测性的),您必须首先从Doctrine中获取
author
对象或
author
ID:

$authorObj = $this->getDoctrine()->getManager()
    ->getRepository('AcmeBundle:Author')->findOneByName(urldecode($author));
然后使用关联数组将ID或Author对象传递到条件中:

$criteria = array('author' => $authorObj);
如果
$author
实际上是作者的ID,则您可以执行以下操作:

$criteria = array('author' => $author);
请注意,即使只得到一个结果,也会得到一个对象集合。您应该使用
findOneBy

public function findPostBy(array $criteria)
{
    return $this->postRepository->findOneBy($criteria);
}
您是否尝试过通过(数组('author'=>$author))查找
->findBy