我试着四处搜索,甚至在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,
));
}
我建议您在实体的存储库中创建自己的查询方法,例如:
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();
}
}