如何从外部文件添加额外的字段和相关的getter / setter到一个或多个Symfony / Doctrine实体,而不继承? [英] How to add extra fields and related getters/setters from an external file to one or many Symfony/Doctrine Entities, without inheritance?

查看:114
本文介绍了如何从外部文件添加额外的字段和相关的getter / setter到一个或多个Symfony / Doctrine实体,而不继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为一堆实体/类添加一些额外的字段,我想获取每个实体/类中包含的字段。
我可以编写每个类中的所有字段,它将工作,但我正在寻找一种方法来集中字段和相关方法的定义,只是为了使其更易于维护,让我分发修改,只需编辑一个文件。
我使用继承工作,但是以这种方式,将会有一个父类/表,它将跟踪* @ InheritanceType(SINGLE_TABLE)*或 @ InheritanceType(JOINED),这可能会显着影响性能,因为记录数量会增加。
我在这里也没有使用继承的优势。

I need to add some extra fields to a bunch of entities/classes and i would like to get those fields included in each entity/class. I could write all the fields in each class and it will work but i'm looking for a way to centralize the definitions of the fields and related methods, just to make it more maintainable and let me distribute modification, just editing a single file. I got it working using inheritance but in that manner, there will be a parent class/table, which will keep track of the children classes/tables either with *@InheritanceType("SINGLE_TABLE")* or @InheritanceType("JOINED") and this could dramatically affect the performances, as the number of records will increase. I do not have any advantage in using inheritance here, as well.

澄清问题的一个简单的例子:

我想包含在某些类中的字段代码

filename: myfields.php

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(name="created_at",type="datetime")
 */
protected $createdAt;

/**
 * @ORM\Column(name="modified_at", type="datetime")
 */
protected $modifiedAt;

public function getId()
{
    return $this->id;
}

public function setCreatedAt($createdAt)
{
    $this->createdAt = $createdAt;
    return $this;
}

public function getCreatedAt()
{
    return $this->createdAt;
}

public function setModifiedAt($modifiedAt)
{
    $this->modifiedAt = $modifiedAt;
    return $this;
}

public function getModifiedAt()
{
    return $this->modifiedAt;
}






filename: Car.php

@ORM\Entity()
@ORM\Table(name="car")
class Car 
{
    /**
     * @ORM\Column(name="model", type="string")
     */
    protected model;

   /**
     * @ORM\Column(name="price", type="decimal")
     */
    protected price;

    // related setter and getters methods

    //Include myfields.php
}






filename: Animal.php

@ORM\Entity()
@ORM\Table(name="animal")
class Animal 
{
    /**
     * @ORM\Column(name="species", type="string")
     */
    protected species;

    // related setter and getters methods

    //Include myfields.php
}






我不想像 类动物扩展MyFields 但我只想导入并存储每个实体表中的公共信息。
如果我需要添加一些外场或只是修改一些方法,我将只修改myfields.php,它将被传播到包含它的所有实体。


I don't want to make something like class Animal extends MyFields but I would like just to import and let store the common informations in each entity table. If i will need to add some extra-field or just to modify some method, i will modify just "myfields.php" and it will be propagated to all the Entities that include it.

肯定在symfony我必须启动: php应用程序/控制台原则:模式:更新--force
和结构将修改包含它的所有类。

For sure in symfony i have to launch: php app/console doctrine:schema:update --force and the structure will be modified fo all the classes that include it.

推荐答案

您可以使用traits来解决此问题(当您使用PHP 5.4 +)。这将允许您的属性和访问器的水平重用,也称为多重继承。您可以简单地定义所有您在特征中的所有常见字段,然后导入您消费类中的特征。

You can solve this problem using traits (when you are using PHP 5.4+). This will allow for horizontal reuse of your properties and accessors, also called multiple inheritance. You can simply define all you common fields in the Trait and import the Trait in your consuming class.

Trait CommonFieldsTrait
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="created_at",type="datetime")
     */
    protected $createdAt;

    /**
     * @ORM\Column(name="modified_at", type="datetime")
     */
   protected $modifiedAt;

   public function getId()
   {
       return $this->id;
   }

   ...
}

@ORM\Entity()
@ORM\Table(name="animal")
class Animal 
{
    use CommonFieldsTrait;

    /**
     * @ORM\Column(name="species", type="string")
     */
    protected species;

    // related setter and getters methods
}

这篇关于如何从外部文件添加额外的字段和相关的getter / setter到一个或多个Symfony / Doctrine实体,而不继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆