简体   繁体   中英

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. 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. 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. 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.

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 ).

  • 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.

  • Replace in the controller(s), that you override GuestEntity with ( MyGuestEntity )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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