wiim23/custom-index-bundle

控制实体自定义索引的注解和命令

安装: 39

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 15

类型:symfony-bundle

v0.2.6 2018-11-20 15:38 UTC

This package is not auto-updated.

Last update: 2024-09-19 19:48:06 UTC


README

CustomIndexBundle 允许使用注解和命令行工具为 doctrine 实体创建索引。

安装

CustomIndexBundle 需要 Symfony 2.1 或更高版本。目前只支持 PostgreSQL。

composer.json 文件中添加包依赖

{
   "require": {
       "intaro/custom-index-bundle": "~0.2.2",
   }
}

安装包

$ composer update intaro/custom-index-bundle

AppKernel 中注册包

// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        //...

        new Intaro\CustomIndexBundle\IntaroCustomIndexBundle(),
    );

    //...
}

如果你的项目在一个数据库中拥有多个模式,并且命令必须只为一个模式生成自定义索引,那么在 config.yml 中添加以下内容

intaro_custom_index:
    search_in_all_schemas: false

search_in_all_schemas 的默认值是 true。如果你在不同的模式中有不同的实体,并且需要一次性更新所有模式中的自定义索引,则必须将 search_in_all_schemas 设置为 true 或省略此配置。如果你只有公共模式的数据库,则 search_in_all_schemas 的值无关紧要。

使用方法

  1. 在你的实体中添加注解
<?php

namespace Acme\MyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Intaro\CustomIndexBundle\Annotations as CustomIndexAnnotation

/**
 * @ORM\Table(name="my_entity")
 * @ORM\Entity()
 * @CustomIndexAnnotation\CustomIndexes(indexes={
 *     @CustomIndexAnnotation\CustomIndex(columns="my_property1"),
 *     @CustomIndexAnnotation\CustomIndex(columns={"lower(my_property1)", "lower(my_property2)"})
 * })
 */
class MyEntity
{
    /**
     * @ORM\Column(type="string", length=256)
     */
    protected $myProperty1;

    /**
     * @ORM\Column(type="string", length=256)
     */
    protected $myProperty2;
}

可用的 CustomIndex 属性

  • columns - 表的列数组
  • name - 索引名称(默认 = 'i_cindex_<所有 CustomIndex 属性的 md5 哈希>')。
  • unique - 索引是否唯一(默认 = false)。
  • using - 对应 PostgreSQL CREATE INDEX 命令中的 USING 指令。
  • where - 对应 PostgreSQL CREATE INDEX 命令中的 WHERE 指令。

只需要 columns 属性。

  1. 使用 intaro:doctrine:index:update 命令来更新数据库。
php app/console intaro:doctrine:index:update

你可以使用 dump-sql 参数来导出带有 DROP/CREATE INDEX 命令的 SQL。

php app/console intaro:doctrine:index:update --dump-sql

一些注解示例

使用 pg_trgm 扩展创建索引

@CustomIndexAnnotation\CustomIndex(columns="lower(my_column) gist_trgm_ops", using="gist")

使用 PostgreSQL 函数创建唯一索引

@CustomIndexAnnotation\CustomIndex(columns={"lower(my_column1)", "nullif(true, not my_column2 isnull)"}, unique=true)

创建部分索引

@CustomIndexAnnotation\CustomIndex(columns={"site_id"}, where="product_id IS NULL")