Creating Custom Field with FOSUserBundle

邮差的信 提交于 2019-12-05 13:33:28

Create an own user bundle and in the MyCompanyUserBundle.php you set

public function getParent(){
        return 'FOSUserBundle';
}

Then in your new UserBundle you create a User entity and let it extend from the base user of the FOS user bundle:

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="user")
*/
class User extends BaseUser
{

    public function __toString(){
        return $this->firstname . ' ' . $this->lastname . ' (' . $this->email . ')';
    }

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



}

EDIT

Set the user in the config as well:

fos_user:
        db_driver: orm
        firewall_name: main
        user_class: MyCompany\UserBundle\Entity\User

the problem is in : when i follow the symfony documentation:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md in step a) Doctrine ORM User class i have created a folder doctrine contains User.orm.yml:

src/Acme/UserBundle/Resources/config/doctrine/User.orm.yml

Acme\UserBundle\Entity\User: type: entity table: fos_user id: id: type: integer generator: strategy: AUTO

this file crush any update in your entities and update of your database shemea .The solution is to delete this folder.

You don't have to add custom fields but you have to create your own user model.

Read the doc, all is explained: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md#step-3-create-your-user-class

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