php - Doctrine 2 索引继承

标签 php inheritance doctrine-orm indexing

我是 Doctrine 2 的新手,我正在尝试弄清楚如何使用它进行索引继承。

我想要实现的是拥有一个基类,该基类定义了一些默认列以及应用程序中所有实体的必要索引。

示例:我的应用程序中的所有表都有 created_onmodified_on,所以我准备了一个包含这两列的基础 @MappedSuperclass .

这是我的代码:

<?php

/**
 * @MappedSuperclass
 */
abstract class EntityAbstract
{
    /**
     * @Column(type="datetime", name="created_on", nullable=false)
     */
    protected $createdOn;

    /**
     * @Column(type="datetime", name="modified_on", nullable=true)
     */
    protected $modifiedOn;

}

/**
 * @Entity
 * @Table(name="member", indexes={@Index(name="credential", columns={"email_address", "password"})})
 */
class Member extends EntityAbstract
{
    /**
     * @Column(type="string", name="full_name", length=50)
     */
    protected $fullName;

    /**
     * @Column(type="string", name="email_address", length=50, unique=true, nullable=false)
     */
    protected $emailAddress;

    /**
     * @Column(type="string", name="password", length=40, nullable=false)
     */
    protected $password;       

}
?>

我想强制 created_on 成为一个索引,所以我在这个特定列的基类中放置了 @Index 注释。希望这将为 Member 生成 2 个索引,即 created_onemail_address+password 组合。然而,这会导致基类的索引被子类覆盖,因此 created_on 不是索引。

/**
 * @MappedSuperclass
 * @Table(indexes={@Index(name="timestampcreated", columns={"created_on"})})
 */
abstract class EntityAbstract

我如何在 Doctrine 2 中实现这一目标?看过单表继承,但我的理解是它用于不同的目的。

最佳答案

这是在 https://github.com/doctrine/orm/issues/5928#issuecomment-273624392 列出的方式的完整解决方案, 感谢 https://medium.com/@alexkunin/doctrine-symfony-adding-indexes-to-fields-defined-in-traits-a8e480af66b2

namespace App\EventListener;

use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use App\Entity\Member;

class MemberIndexListener
{
    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
    {
        /** @var Doctrine\ORM\Mapping\ClassMetadata $classMetadata */
        $classMetadata = $eventArgs->getClassMetadata();
        if (Member::class === $classMetadata->getName())
        {
            $prefix = strtolower((new \ReflectionClass($classMetadata->getName()))->getShortName()); // you can omit this line and ...
            $classMetadata->table['indexes'][$prefix.'_created_on'] =  // ... replace `[$prefix.'_created_on']` with `[]` for automatic index naming
                ['columns' => ['username']
            ];
            // For UniqueConstraints, use:
            // $classMetadata->table['uniqueConstraints'][...] = ...
        }
    }
}

services.yaml 中:

services:
    App\EventListener\MemberIndexListener:
        tags:
            - { name: doctrine.event_listener, event: loadClassMetadata }

关于php - Doctrine 2 索引继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20557932/

相关文章:

php - 如何将多次出现的子模式捕获到一次捕获中?

PHP Soap 客户端 : How call WebService with Derived class as parameter?

php - Symfony Doctrine 数组结果的 Flatten Array Result

symfony - Doctrine OneToOne 删除实体

php - 搜索功能: Error

php - 我可以在 PHP 中混合使用 MySQL API 吗?

java - 使用继承和 Java 接口(interface)使用为 Java 创建 XML 模式

c++ - 从继承的函数指针调用基方法

java - 属性和多态性

php - Doctrine2 性能 : Insert/Update multiple rows