数组字段内的 Symfony 2 自定义 DQL 请求

Symfony 2 custom DQL request inside an array field

我正在尝试在我的存储库中使用数组字段内的 WHERE 子句发出自定义请求。我尝试过类似的方法,但没有用,但可以更好地说明我的问题:

$qb ->andWhere( "p.addresses[:index] = :address" )
    ->setParameter( "index" , $p_idLang )
    ->setParameter( "address" , $p_address );

摘自documentation关于数组类型:

Maps and converts array data based on PHP serialization. If you need to store an exact representation of your array data, you should consider using this type as it uses serialization to represent an exact copy of your array as string in the database. Values retrieved from the database are always converted to PHP’s array type using deserialization or null if no data is present.

您的查询没有意义。不过你有几个选择:

  1. 检索 p.adresses 并使用 php 检查是否 p.adresses[$index] = $address
  2. 尝试一些不太可靠但可行的方法:

$val_length = strlen($p_address); $qb ->andWhere( "p.addresses LIKE :indexAddress" ) ->setParameter( "indexAddress" , "%i:$p_idLang;s:$val_length:$p_address%" );

  1. 创建一个新实体以及该实体与新实体之间的 oneToMany 关系。

我肯定会尝试选项 3。如果数组很大或将来会变大,则选项 1 不是一个选项。我不会选择选项 2,但作为一个实验可能值得一试。