I am extending the Sonata User Bundle and creating some extra fields in the new user entity. These fields will only be updated within the Sonata admin area under users so they do not need to be available in the edit profile form. I am having trouble updating these fields via the Sonata User Manager and tried several different ways to extend/implement that class in Application\Sonata\UserBundle. Has anyone encountered this before and can give me a tutorial or step by step process of the cleanest way to extend the new User entity?
Something like AcmeUserBundle. Create it and register it as you do normally.
Then create a User
and Group
entity which extends Sonata\UserBundle\Entity\BaseUser
and Sonata\UserBundle\Entity\BaseGroup
. You should also add the configuration for the primary key, for instance:
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
}
then, go to your app/config/config.yml
file and configure these new entities:
sonata_user:
class:
user: Acme\UserBundle\Entity\User
group: Acme\UserBundle\Entity\Group
Then, you need to create a new UserAdmin class. To do this, just create a new UserAdmin
class inside your bundle, extend Sonata\UserBundle\Admin\Model\UserAdmin
and override the methods like this:
namespace Acme\UserBundle\Admin;
use Sonata\UserBundle\Admin\Model\UserAdmin as SonataUserAdmin;
class UserAdmin extends SonataUserAdmin
{
/**
* {@inheritdoc}
*/
protected function configureFormFields(FormMapper $formMapper)
{
parent::configureFormFields($formMapper);
$formMapper
->with('new_section')
->add(...)
// ...
->end()
;
}
}
Then, you need to make sure Sonata uses the new UserAdmin class. You just need to set the sonata.user.admin.user.class
parameter to your new class and your ready!
# app/config/config.yml
parameters:
sonata.user.admin.user.class: Acme\UserBundle\Admin\UserAdmin
I found out the issue was a doctrine issue. My extended bundle was utilizing the original xml field mappings. I deleted those files and reverted to annotations. Everything worked brilliantly from there. I hope this helps someone else who is experiencing the same issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With