Doctrine和Postgres数组

use*_*498 5 arrays postgresql doctrine

我发现Doctrine数组类型使用序列化/反序列化并将数据存储在"简单"字段中,如text,integer.

但我有一个带有postgres数据库的项目,其中一些字段为数组(主要是文本).我想将该数据库与Doctrine一起使用.有办法处理吗?

谢谢

编辑:

ps:我现在知道sql数组可以是一个很好的做法,感谢Craig Ringer.

也许我可以创建自定义类型.是否有人已经做到了?

编辑2:

问题解决了一半:

<?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
    }
}
Run Code Online (Sandbox Code Playgroud)

和控制器

<?php

namespace mysite\MyBundle;

 use Symfony\Component\HttpKernel\Bundle\Bundle;
 use Doctrine\ORM\EntityManager;
 use Doctrine\DBAL\Types\Type;

 class mysiteMyBundle extends Bundle
{
    public function boot() {
        Type::addType('textarray', 'mysite\MyBundle\Types\TextArray');
        $em = $this->container->get('doctrine.orm.default_entity_manager');
        $conn = $em->getConnection();
        $conn->getDatabasePlatform()->registerDoctrineTypeMapping('TEXTARRAY', 'textarray');
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道想在这些数组中搜索像'myword'= ANY(myarrayfield)这样的查询...是否有人有线索?

#doctrine2频道上的某个人告诉我构建一个自定义DQL函数,所以这里是:

<?php
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;

class ANY extends FunctionNode {

    public $field;


    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) {
        return 'ANY(' .
            $this->field->dispatch($sqlWalker) . ')';
    }

    public function parse(\Doctrine\ORM\Query\Parser $parser) {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $this->field = $parser->StringPrimary();
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我使用查询构建器构建我的查询,其中包含类似的数组

$arr[] = $qb->expr()->like($alias.".".$field, $qb->expr()->literal('%'.trim($val).'%') );
Run Code Online (Sandbox Code Playgroud)

加入他们

if(count($arr) > 0) $qb->Where(new Expr\Orx($arr));
else unset($arr);
Run Code Online (Sandbox Code Playgroud)

那么,如何使用我的功能?:\

小智 5

在 QueryBuilder 中,您可以通过编写自定义函数来使用它们:

$qb->where("ANY(a.b)");
Run Code Online (Sandbox Code Playgroud)

一些进一步的信息:

  1. 您可以将自定义类型添加到 Symfony 应用程序中的“doctrine”配置部分。
  2. 这同样适用于自定义 DQL 函数。

有关 Symfony 2.1 中的语法,请参阅 CLI 上的“config:dump-reference DoctrineBundle”命令,否则请参阅 symfony.com 网站。