wobal / custom-index-bundle

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

安装: 9,263

依赖: 0

建议者: 0

安全: 0

星级: 0

关注者: 1

分支: 15

类型:symfony-bundle

v0.3.0 2023-11-27 11:38 UTC

This package is auto-updated.

Last update: 2024-09-27 13:25:12 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")