pjarmalavicius/entity-extend-bundle

简单的 doctrine 实体扩展

1.0 2016-01-31 13:43 UTC

This package is not auto-updated.

Last update: 2024-09-20 11:14:47 UTC


README

PjEntityExtendBundle 是一个 symfony2 插件,允许扩展 doctrine ORM 实体。当您想为其他实体声明通用信息时,Doctrine MappedSupperclass 很有用,但每当创建一个新的子类时,它都必须是一个新的实体。有时您需要扩展现有的实体,而不引入新的实体。PjEntityExtendBundle 可以帮助您。

安装

Composer

composer require pjarmalavicius/entity-extend-bundle

启用插件

// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        // ...
        new \Pj\EntityExtendBundle\PjEntityExtendBundle(),
        // ...
    );
}

用法

PjEntityExtendBundle 可以与注解或 yml 映射一起使用。假设您有一个 Product 实体

<?php

namespace Acme\ProductBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Class BaseProduct.
 *
 * @ORM\Entity
 * @ORM\Table(name="products")
 */
class BaseProduct
{
    /**
     * @var int
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     * @ORM\Column(type="string", length=100)
     */
    protected $name;

    /**
     * Getter for name.
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Setter for name.
     *
     * @param string $name
     *
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;
        
        return $this;
    }
}

并且您想向它添加额外的字段(比如描述),而不更改 BaseProduct 类。从面向对象的角度来看,这很简单

<?php

namespace Custom\ProductBundle\Entity;

use Acme\ProductBundle\Entity\Product as BaseProduct;
use Doctrine\ORM\Mapping as ORM;

/**
 * Class CustomProduct.
 *
 * @ORM\Entity
 */
class CustomProduct extends BaseProduct
{
    /**
     * @var string
     * @ORM\Column(type="string", nullable=true)
     */
    protected $description;
    
    /**
     * Getter for description.
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Setter for description.
     *
     * @param string $description
     *
     * @return $this
     */
    public function setDescription($description)
    {
        $this->description = $description;
        
        return $this;
    }
}

但现在 doctrine 会认为 CustomProduct 是一个新的实体,有自己的数据库表,这是一个问题,因为我们想 CustomProduct 是同一个产品实体,并使用相同的数据库表。使用 PjEntityExtendBundle 可以解决这个问题。首先,您需要将扩展实体列表添加到您的 config.yml 配置中

pj_entity_extend:
    extended_entities:
        Acme\ProductBundle\Entity\BaseProduct: Custom\ProductBundle\Entity\CustomProduct

第二步是说明 CustomProduct 扩展 BaseProduct 的映射信息。

注解映射

如果您使用注解映射,需要向 CustomProduct 类添加 @ExtentedEntity 注解

/**
 * Class CustomProduct.
 *
 * @ORM\Entity
 * @Pj\ExtendedEntity(className="Acme\ProductBundle\Entity\BaseProduct")
 */
class Product extends BaseProduct
{
    //...

yml 映射

如果您使用 yml 映射,需要向 CustomProduct.yml 添加 extended_entity 属性

Custom\ProductBundle\Entity\CustomProduct:
    extended_entity: Acme\ProductBundle\Entity\BaseProduct
    type: entity
    fields:
        description:
            type: text
            nullable: true

就是这样。现在 doctrine 会知道,CustomProduct 与相同的 products 数据库表相关联。如果您使用 doctrine 迁移,生成的迁移将向 products 表添加 descrition 字段,而不是创建新的数据库表。

当然,PjEntityExtendBundle 不仅可以用作添加新字段,还可以用于覆盖已定义的,例如增加 name 字段的长度。