Domanda

I want to extend default user entity attributes. In database after extending FOSUserBundle with SonataUserBunle there are 2 tables for storing users: fos_user and fos_user_user. I want to extend fos_user

Here is app/AppKernel.php:

new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'),
new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),

in app/config.yml file I set:

fos_user:
    db_driver:       orm<br>
    firewall_name:   main<br>
    user_class:      Acme\DemoBundle\Entity\User<br> 
    group:<br>
        group_class: Application\Sonata\UserBundle\Entity\Group<br>

sonata_user:
    security_acl:    true

    class:
        user:        Acme\DemoBundle\Entity\User
    admin:
        user:
            class:   Acme\DemoBundle\Admin\UserAdmin
parameters:
    sonata.user.admin.user.class: Blogger\BlogBundle\Admin\UserAdmin
    sonata.user.admin.user.entity: Blogger\BlogBundle\Entity\User

doctrine:
    orm:
        auto_mapping: auto

SonataUserBundle is created in src/Application/Sonata/UserBundle
To override SonataUserBundle, i extended UserAdmin by creating another UserAdmin class in src/Acme/DemoBundle/Admin/UserAdmin and all works fine
Now I want to extend User entity (fos_user table) to add new attributes.
Here is my Acme/Demo/Entity/User.php that I want to extend the default User entity

namespace Acme\DemoBundle\Entity;

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

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

    /*
     * @ORM\Column(type="string", name="newAttribute")
     */
    protected $newAttribute;


    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

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


What should I do to extend default fos_user table with php app/console doctrine:generate:entities Acme/DemoBundle

I've read many posts about this issue but none of the solutions helped me. I tried to extend Model class instead of Entity class but also nothing changed

È stato utile?

Soluzione

You don't have to.

new Sonata\UserBundle\SonataUserBundle('FOSUserBundle')

Here your are telling the SonataUserBundle to use the FOSUserBundle as well.

Then in your User entity you set:

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

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

Because you have said to the SonataUserBundle "use the fosuserbundle" and you extend from the sonata user bundle user entity he's gonna "merge" those 2 models into 1 and add your custom fields from in your user entity as well.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top