1

I'm using SonataUserBundle without extending FOSUserBundle. I wanted to change the default SonataUser entity, and add or remove some fields.

here is the default User Entity which has been created with SonataEasyExtendsBundle

<?php

/**
 * This file is part of the <name> project.
 *
 * (c) <yourname> <youremail>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Application\Sonata\UserBundle\Entity;

use Sonata\UserBundle\Entity\BaseUser as BaseUser;

/**
 * This file has been generated by the Sonata EasyExtends bundle.
 *
 * @link https://sonata-project.org/bundles/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 int $id
     */
    protected $id;

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

    public function __toString()
    {
        if (parent::getFirstname() != null){
            return parent::getFullname();
        } else {
            return parent::getUsername();
        }
    }
}
Aien Saidi
  • 159
  • 7
  • 25

2 Answers2

1

I think you can add fields, methods and mapping just like to any other entity, but I'm afraid you can't remove fields (but you can hide them in a custom admin). The mapping files are in your Application directory.

greg0ire
  • 22,714
  • 16
  • 72
  • 101
1

Ok, found the solution, if anyone out there has this problem, this could fix it

these are the procedures:

  1. Remove the doctrine directory, after extending User entity via SonataEasyExtends. By default you can find it in Application/Sonata/UserBundle/Resources/config/doctrine
  2. Config User entity to match your needs, this is also in your Application folder. Your User entity must be like this before you add or remove anything:

namespace Application\Sonata\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;

/**
 * User
 *
 * @ORM\Table(name="fos_user_user")
 * @ORM\Entity
 */
class User extends BaseUser
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

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

    public function __toString()
    {
        if (parent::getFirstname() != null){
            return parent::getFullname();
        } else {
            return parent::getUsername();
        }
    }
}
  1. Generate Admin Class, just using app/console sonata:admin:generate Application/Sonata/UserBundle/Entity/User

  2. override Admin Class, easily copy and paste UserAdmin.php from Sonata\UserBundle\Admin\Model to your newly created Admin Class

Optional

  1. there is this issue #418 which could be fixed by changing UserAdmin:182 from if ($this->getSubject() && !$this->getSubject()->hasRole('ROLE_SUPER_ADMIN')) to if ($this->getSubject() && $this->getSubject()->hasRole('ROLE_SUPER_ADMIN'))
Aien Saidi
  • 159
  • 7
  • 25