Search code examples
symfonyfosuserbundlesymfony-sonata

Extending the User entity


I'm trying to extend the User Entity of the sonata user bundle, but it always fails (at least when I want to use ist with the sonata admin bundle). If I use the Application/Sonata/UserBundle/Entity/User Entity it works and I can edit it in the dashboard.

My Entity looks like this:

<?php

namespace MyNamespace\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityManager;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;

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

In my config.yml I defined the following options:

fos_user:
    db_driver: orm
    firewall_name: main
    user_class:     MyNamespace\MyBundle\Entity\User
    group:
        group_class:   Application\Sonata\UserBundle\Entity\Group
        group_manager: sonata.user.orm.group_manager
    service:
    user_manager: sonata.user.orm.user_manager 

And:

sonata_user:
security_acl: true
manager_type: orm
class:
   user: MyNamespace\MyBundle\Entity\User

The login is working correctly, but then I get the error message

"An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "admin_sonata_user_user_create" as such route does not exist.") in SonataAdminBundle:Block:block_admin_list.html.twig at line 39."

My routing.yml looks like this:

admin:
    resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
    prefix: /

_sonata_admin:
    host:       www.mydomain.com
    resource: .
    type: sonata_admin
    prefix: /
    schemes:    [https]

Even if I follow this tutorial (Extending Sonata User Bundle and adding new fields) step by step I get the same error.


Solution

  • Application/Sonata/UserBundle/Entity/User is extended Sonata/UserBundle/Entity/User so why not just to make changes there instead of another class?

    It's already extended bundle so you can make all the changes there. I put my additional fields there.

    My file Application/Sonata/UserBundle/Entity/User.php is like:

    namespace Application\Sonata\UserBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    use Sonata\UserBundle\Entity\BaseUser as BaseUser;
    
    /**
     * User
     * @ORM\Entity
     * @ORM\Table(name="fos_user_user")
     */
    class User extends BaseUser
    {
        /**
         * @var integer
         * @ORM\Column(type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;
    
        /**
         * @var string
         */
        private $company;
    

    If your really want to use another class you need to take care about routing.

    Check \vendor\sonata-project\user-bundle\Resources\config\admin_orm.xml - it's the place where original class is added as a service.

    You can see there %sonata.user.admin.user.entity% parameter - you could change this in config if you want to change to another class. I think it's better than changing fos_user.user_class.

    You can also move the whole file to your Application/Sonata/UserBudle but then you need to load it inside DependencyInjection/ApplicationSonataUserExtension.php

    Btw, this is really tricky in Sonata which user bundles as Paweł wrote and takes a lot of debugging to make it work