简体   繁体   中英

Symfony2, Sonata MediaBundle : add new fields to table

I'm trying to add 4 new fields in Sonata MediaBundle for the GalleryHasMedia.

I correctly override the GalleryHasMediaAdmin :

GalleryHasMedia

To override it i added in services.yml this line :

parameters:
    sonata.media.admin.gallery_has_media.class: Application\Sonata\MediaBundle\Admin\GalleryHasMediaAdmin

I had to create the methods manually (getName and else) since php app/console doctrine:generate:entities ApplicationSonataMediaBundle:GalleryHasMedia apparently not caring about my new fields set in my custom entity Application\\Sonata\\MediaBundle\\Entity\\GalleryHasMedia.

As well --dump-sql return "Nothing to update". But the methods (getName and else) are correctly recognize in the Sonata admin, so why not the new fields?

here my custom entity :

<?php
namespace Application\Sonata\MediaBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Sonata\MediaBundle\Entity\BaseGalleryHasMedia as BaseGalleryHasMedia;

/**
 * @ORM\Entity
 * @ORM\Table(name="media__gallery_media")
 */
class GalleryHasMedia extends BaseGalleryHasMedia
{

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

    /**
     * @ORM\Column(type="string", length=64, nullable=false, name="name")
     **/
    private $name;

    /**
     * @ORM\Column(type="string", length=64, nullable=false, name="activity")
     **/
    private $activity;

    /**
     * @ORM\Column(type="text", nullable=false, name="description")
     */
    private $description;

    /**
     * @ORM\Column(type="string", length=255, nullable=false, name="code")
     **/
    private $link;

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

    /**
     * Set name
     *
     * @param string $name
     * @return GalleryHasMedia
     */
    public function setName($name)
    {
         $this->name = $name;

        return $this;
    }

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

    /**
     * Set activity
     *
     * @param string $activity
     * @return GalleryHasMedia
     */
    public function setActivity($activity)
    {
        $this->activity = $activity;

        return $this;
    }

    /**
     * Get activity
     *
     * @return string
     */
    public function getActivity()
    {
        return $this->activity;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return GalleryHasMedia
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set link
     *
     * @param string $link
     * @return GalleryHasMedia
     */
    public function setLink($link)
    {
        $this->link = $link;

        return $this;
    }

    /**
     * Get link
     *
     * @return string
     */
    public function getLink()
    {
         return $this->link;
    }

}

And i correctly set as said in their Documentation :

sonata_media:
    # if you don't use default namespace configuration
    class:
        media: Application\Sonata\MediaBundle\Entity\Media
        gallery: Application\Sonata\MediaBundle\Entity\Gallery
        gallery_has_media: Application\Sonata\MediaBundle\Entity\GalleryHasMedia

I'm using auto mapping so my custom entity is correctly mapped :

[OK] Application\\Sonata\\MediaBundle\\Entity\\GalleryHasMedia

here the actual table (sonata's default table) :

奏鸣曲的默认表

So any ideas why i can't add any new fields to the gallery_has_media table?

UPDATE :

I'm guessing it is because i'm using annotations. How can i keep using annotations and makes it sync with my database?

This guy encountered a similar problem Issue

Okay, i found the answer correctly explained here .

Deleting Application/Sonata/MediaBundle/Resources/config/doctrine allowed me to use annotations inside my custom entity.

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