wiim23 / custom-index-bundle
控制实体自定义索引的注解和命令
v0.2.6
2018-11-20 15:38 UTC
Requires
- php: >=7.1.3
- doctrine/orm: ^2.5
- symfony/config: ^4.1
- symfony/console: ^4.1
- symfony/dependency-injection: ^4.1
- symfony/http-kernel: ^4.1
- symfony/validator: ^4.1
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
的值无关紧要。
使用方法
- 在你的实体中添加注解
<?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
- 对应 PostgreSQLCREATE INDEX
命令中的USING
指令。where
- 对应 PostgreSQLCREATE INDEX
命令中的WHERE
指令。
只需要 columns
属性。
- 使用
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")