Arrays 条令与Postgres数组
我发现条令数组类型使用序列化/非序列化,并将数据存储在“简单”字段中,如text、integer 但我有一个项目,有一个postgres数据库,其中一些字段是数组(主要是文本)。我想把那个数据库和条令一起使用。有办法处理吗 谢谢 编辑: ps:多亏了Craig Ringer,我现在知道sql数组是一个很好的实践 也许我可以创建一个自定义类型。有人已经做过了吗 编辑2: 问题解决了一半:Arrays 条令与Postgres数组,arrays,postgresql,doctrine,Arrays,Postgresql,Doctrine,我发现条令数组类型使用序列化/非序列化,并将数据存储在“简单”字段中,如text、integer 但我有一个项目,有一个postgres数据库,其中一些字段是数组(主要是文本)。我想把那个数据库和条令一起使用。有办法处理吗 谢谢 编辑: ps:多亏了Craig Ringer,我现在知道sql数组是一个很好的实践 也许我可以创建一个自定义类型。有人已经做过了吗 编辑2: 问题解决了一半: <?php namespace mysite\MyBundle\Types; use Doctrine
<?php
namespace mysite\MyBundle\Types;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* My custom postgres text array datatype.
*/
class TEXTARRAY extends Type
{
const TEXTARRAY = 'textarray'; // modify to match your type name
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getDoctrineTypeMapping('TEXTARRAY');
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
// This is executed when the value is read from the database. Make your conversions here, optionally using the $platform.
return explode('","', trim($value, '{""}') );
}
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
// This is executed when the value is written to the database. Make your conversions here, optionally using the $platform.
settype($value, 'array'); // can be called with a scalar or array
$result = array();
foreach ($set as $t) {
if (is_array($t)) {
$result[] = to_pg_array($t);
} else {
$t = str_replace('"', '\\"', $t); // escape double quote
if (! is_numeric($t)) // quote only non-numeric values
$t = '"' . $t . '"';
$result[] = $t;
}
}
return '{' . implode(",", $result) . '}'; // format
}
public function getName()
{
return self::TEXTARRAY; // modify to match your constant name
}
}
和他们一起
if(count($arr) > 0) $qb->Where(new Expr\Orx($arr));
else unset($arr);
那么,如何使用我的函数呢\ 在QueryBuilder中,您只需编写自定义函数即可使用它们:
$qb->where("ANY(a.b)");
一些进一步资料:
$arr[] = $qb->expr()->like($alias.".".$field, $qb->expr()->literal('%'.trim($val).'%') );
if(count($arr) > 0) $qb->Where(new Expr\Orx($arr));
else unset($arr);
$qb->where("ANY(a.b)");