Symfony2 Doctrine查询内爆传递的无效参数

时间:2015-03-30 14:36:22

标签: php symfony doctrine twig

我已经尝试过搜索Symfony2文档中的示例,并且一直在努力编写查询以按帖子类别选择所有博客帖子,并按ID按降序排序。

但是,当我运行代码时,我有以下错误。有什么建议吗?

ContextErrorException in SimpleArrayType.php line 51:
Warning: implode(): Invalid arguments passed

我正在创建一个博客,用于学习目的,并尝试使用以下列从我发布的表中检索帖子。

Id |postTitle | postDescription | postContent | postCategory

我的实体看起来像这样,(显示最相关的部分)

 /**
* posted
*
  @ORM\Table()
* @ORM\Entity
*/
class posted
{
 /**
 * @ORM\Column(type="string", length=500)
 */
protected $postTitle;
/**
 * @ORM\Column(type="string", length=500)
 */
protected $postDescription;
/**
 * @ORM\Column(type="string", length=500)
 */
protected $postContent;


/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;
/**
 * @ORM\Column(type="datetime", name="posted_at")
 */
protected $datePosted;


/**
 *@var array
 * @ORM\Column(type="simple_array", length=250)
 */
protected $postCategory;

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

    /**
 * Set postCategory
 *
 * @param string $postCategory
 * @return posted
 */
public function setPostCategory($postCategory)
{
    $this->postCategory = $postCategory;

    return $this;
}

/**
 * Get postCategory
 *
 * @return string 
 */
public function getPostCategory()
{
    return $this->postCategory;
}

我的控制器看起来像

    /**
 * this is the EPL page of posts of EPL category
 *
 * @Route("/EPL", name="eplposts")
 * @Method("GET")
 * @Template()
 */
public function eplAction()
{
    $em = $this->getDoctrine()->getManager();
    $entities = $em->getRepository('BlogBundle:posted')->findBy(array('postCategory' => 'English Premier League','id' => 'desc'));

   return $this->render('BlogBundle:Default:EPLpost.html.twig',array(
        'entities' => $entities,
    ));
}

1 个答案:

答案 0 :(得分:0)

我建议您在实体的存储库中创建自己的查询方法,例如:

class BoardRepository extends EntityRepository
{
    public function findByCategory($category)
    {
        $builder = $this->createQueryBuilder('p');

        $builder
            ->where($builder->expr()->like('p.postCategory', '%'.$category.','))
            ->orWhere($builder->expr()->like('p.postCategory', ','.$category.'%'))
        ;

        return $builder->getQuery()->execute();
    }
}