Doctrine 2中是否有任何方法提示在自动生成数据库时应将列编入索引?我有一个线程实体:
Entities\Thread:
type: entity
table: Thread
repositoryClass: Repositories\ThreadRepository
id:
id:
type: integer
generator:
strategy: AUTO
fields:
type:
type: enumthreadtype
code:
type: string
length: 10
nullable: true
manyToOne:
group:
targetEntity: Group
inversedBy: threads
oneToMany:
messages:
targetEntity: Message
mappedBy: thread
attachments:
targetEntity: ThreadAttachment
mappedBy: thread
线程经常通过他们的“代码”搜索 - 一个10个字符的随机字符串 - 而不是他们的id。有没有办法表明应该使用yaml创建索引,还是只需要在创建表后在我的数据库中执行此操作?
答案 0 :(得分:1)
我无法在yaml中找到相关文档,但在阅读Doctrine\ORM\Mapping\ClassMetadatainfo
之后我发现它如下:
如果您想索引代码字段,您的yaml映射将是
Entities\Thread:
type: entity
table: Thread
indexes:
# _idx as suffix...
thread_code_idx:
columns: [ code ]
# We can specify multiple indexes
another_idx
# we can specify multiple columns for compound indexes
columns [ type, code ]
repositoryClass: Repositories\ThreadRepository
id:
id:
type: integer
generator:
strategy: AUTO
fields:
type:
type: enumthreadtype
code:
type: string
length: 10
nullable: true
manyToOne:
group:
targetEntity: Group
inversedBy: threads
oneToMany:
messages:
targetEntity: Message
mappedBy: thread
attachments:
targetEntity: ThreadAttachment
mappedBy: thread