Symfony2 FosUserBundle and SonataUserBundle : over

2019-04-11 21:25发布

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

2条回答
叼着烟拽天下
2楼-- · 2019-04-11 21:59

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

查看更多
我命由我不由天
3楼-- · 2019-04-11 22:09

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

查看更多
登录 后发表回答