Symfony2 FosUserBundle and SonataUserBundle : overriding entities?

女生的网名这么多〃 提交于 2019-12-21 20:28:16

问题


I use FosUserBundle and SonataUserBundle for my Symfony2 project.

I get confused now. I want to add fields for the entity User but it's not working. There is no update for the schema for example.

Here is my config :

AppKernel:
...
new FOS\UserBundle\FOSUserBundle(),
new Sonata\UserBundle\SonataUserBundle(),
new Application\Sonata\UserBundle\ApplicationSonataUserBundle('FOSUserBundle')


config.yml:
...
# FOSUserBundle Configuration
fos_user:
    db_driver:     orm                        # BDD type
    firewall_name: main                       # firewall name
    user_class:    Application\Sonata\UserBundle\Entity\User # entity class defined

And the User entity with added fields, in app/Application/Sonata/userBundle/Entity/

namespace Application\Sonata\UserBundle\Entity;

use Sonata\UserBundle\Entity\BaseUser as BaseUser;

/**
 * This file has been generated by the Sonata EasyExtends bundle ( http://sonata-    project.org/easy-extends )
 *
 * References :
 *   working with object : http://www.doctrine-project.org/projects/orm/2.0        /docs/reference/working-with-objects/en
 *
 * @author <yourname> <youremail>
 */
class User extends BaseUser
{

    /**
     * @var integer $id
     */
    protected $id;

    /**
     * @var string
     */
    protected $institution;

    /**
     * @var string
     */
    protected $department;

    /**
     * @var string
     */
    protected $city;

    /**
     * @var string
     */
    protected $country;

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

    /**
     * @return string
     */
    public function getInstitution()
    {
        return $this->institution;
    }

    /**
     * @return string
     */
    public function getDepartment()
    {
        return $this->department;
    }

    /**
     * @return string
     */
    public function getCity()
    {
        return $this->city;
    }

    /**
     * @return string
     */
    public function getCountry()
    {
        return $this->country;
    }
}

In my database, table fos_user_user seems to be the table with user saved data.

Added fields (country, city...) are not created when calling "php app/console doctrine:schema:update --force". How to add fields to the user entity ? I'm lost in fosuserbundle and sonatauserbundle....


回答1:


In fact, it appear that the way i use fos and sonata user bundles force me to use XML definition. So for example adding a field called "city" you have to add these lines :

User.php (in /app/Application/Sonata/UserBundle/Entity/) :

protected $city;

User.orm.xml (in /app/Application/Sonata/UserBundle/Ressource/config/doctrine/) :

<field name="city" column="city" type="text"/>

Maybe putting this in the documentation of the bundles would be interesting for newbies ;)




回答2:


I found a better solution if you want use annotations.

Delete UserBundle/Resorces/config/doctrine folder and you can use annotations in your UserBundle/Entity folder



来源:https://stackoverflow.com/questions/12617682/symfony2-fosuserbundle-and-sonatauserbundle-overriding-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!