MySql Doctrine:找到给定的变量是否为IN数组属性 [英] MySql Doctrine: find if given variable is IN array property

查看:162
本文介绍了MySql Doctrine:找到给定的变量是否为IN数组属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类的任务与类别整数数组属性

I have class Task with categories array of integers property

class Task{
      /**
       * @var array
       *
       * @ORM\Column(name="categories", type="array", nullable=true)
       */
private $categories;
 }

现在在控制器我正在尝试构建查询,将检查是否类别ID 变量位于类别任务数组

now in controller I am trying to build query which would check if category id variable is in categories array of the task

  $qb = $this->getDoctrine()->getRepository('CoreBundle:Task')->createQueryBuilder('t');
  $qb->where(':category IN (t.categories)')
                    ->setParameter('category', $category);

这给我错误:

 [Syntax Error] line 0, col 140: Error: Expected Literal, got "t";


推荐答案

据我所知,这是不可能的直接在Doctrine中,数组在技术上不是一个数组,直到它从数据库中被取消排序。

To the best of my knowledge this isn't possible in Doctrine directly as the array isn't technically an array until it has been unserialized from the database.

我知道获得所需结果的唯一方法是将数据库值作为字符串处理,并使用(使用通配符)搜索该值中所需的字符串。

The only way I know to get the result you are looking for is to treat your database value as a string and search for the required string in that value using a like with wildcards.

$qb = $this->getDoctrine()->getRepository('CoreBundle:Task')->createQueryBuilder('t');
$qb->where('t.categories LIKE :category')
   ->setParameter('category', '%'.$category.'%');

这篇关于MySql Doctrine:找到给定的变量是否为IN数组属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆