简体   繁体   English

Symfony 3.1:如何扩展symfony包的实体?

[英]Symfony 3.1: How to extend the entity of a symfony bundle?

I have implemented a Symfony 3.1 based forum bundle Discutea/DForumBundle and would like to enhance and add features to it. 我已经实现了一个基于Symfony 3.1的论坛捆绑的Discutea / DForumBundle,并希望增强和添加功能。 I have so far tried How to Override any Part of a Bundle ; 到目前为止,我已经尝试过如何覆盖捆绑包的任何部分 ; overriding the views and language packs were possible but I would also like to be able to add new entities and add new controllers too. 覆盖视图和语言包是可能的,但我也希望能够添加新实体并添加新的控制器。

In the case of FOSUserBundle , this was possible as the we could extend the User entity and add our custom preferences to it making the tweak possible. FOSUserBundle的情况下,这是可能的,因为我们可以扩展用户实体并添加我们的自定义首选项,使调整成为可能。 What would be the best way to achieve the same for bundles of this kind? 对于这种捆绑来说,实现相同目标的最佳方法是什么?

Any hint or help would be appreciated. 任何提示或帮助将不胜感激。

====== ADDED DETAILS ====== ======添加细节======

I think extending the entity should be enough for me, for I only want to add few new fields like viewCount and all. 我认为扩展实体对我来说应该足够了,因为我只想添加一些新的字段,比如viewCount和all。 So the following is my new extended entity: 所以以下是我的新扩展实体:

<?php

namespace AppBundle\Entity;

use Discutea\DForumBundle\Entity\Model\BasePost;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ForumPostRepository")
 * @ORM\Table(name="df_post")
 */
class ForumPost extends BasePost
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Column(name="views_count", type="integer", nullable=true)
     */
    protected $viewCounts = 0;


    /**
     * Set viewCounts
     *
     * @param integer $viewCounts
     *
     * @return ForumPost
     */
    public function setViewCounts($viewCounts)
    {
        $this->viewCounts = $viewCounts;

        return $this;
    }

    /**
     * Get viewCounts
     *
     * @return integer
     */
    public function getViewCounts()
    {
        return $this->viewCounts;
    }
}

After this, I tried the schema update with console; 在此之后,我尝试使用控制台进行架构更新; but I got the following error message: 但是我收到以下错误消息:

[Doctrine\\DBAL\\Schema\\SchemaException] The table with name 'my_database.df_post' already exists. [Doctrine \\ DBAL \\ Schema \\ SchemaException]名称为“my_database.df_post”的表已存在。

How to overcome this and update my existing schema? 如何克服这个并更新我现有的架构? After this, I intend to extend other entities as well. 在此之后,我打算扩展其他实体。

First you need to understand do you want just to extend the bundle or you want to change some core functionality of it. 首先,您需要了解您是否只想扩展捆绑包,或者您想要更改它的某些核心功能。

If you want to change core functionality, then I'll suggest you to fork the bundle and directly work on the forked bundle. 如果你想改变核心功能,那么我建议你分叉捆绑并直接处理分叉捆绑。

If you want only to extend entity, then: 如果您只想扩展实体,那么:

  • Create your entity ( MyGuestEntity ) in your bundle extending from the third party bundle entity (for example: GuestEntity ). 在从第三方包实体扩展的包中创建您的实体( MyGuestEntity )(例如: GuestEntity )。

  • Then you need to find where the in the third party bundle the GuestEntity is used and to override this controller(s) in your bundle. 然后,您需要找到第三方包中使用GuestEntity位置,并覆盖捆绑包中的此控制器。

  • Replace in the controller(s), that you override GuestEntity with ( MyGuestEntity ) 在控制器中替换,用( MyGuestEntity )覆盖GuestEntity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM