Extending Sonata User Bundle and Adding New Fields

Extending Sonata User Bundle and adding new fields

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.

Extending Sonata User Bundle and adding new fields

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.

Extending Sonata User Bundle & custom actions

You already configured the service. Now you have to create a UserController that extends the CRUDController and implements your invoicedAction and creditedAction methods.

<?php

namespace AppBundle\Controller;

use AppBundle\Entity\User;
use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

class UserController extends CRUDController
{

/**
*
* @param string $id
* @param Request $request
*
* @return RedirectResponse
*/
public function invoicedAction($id = null, Request $request = null)
{
if ( $request == null ) {
$request = $this->getRequest();
}
$id = $request->get($this->admin->getIdParameter());

$user = $this->admin->getObject($id);
/* @var $user User */

if (!$user) {
throw $this->createNotFoundException(sprintf('unable to find the user with id : %s', $id));
}

$this->admin->checkAccess('invoiced', $user);

$this->admin->setSubject($user);

/// your code here...

return new RedirectResponse($this->admin->generateUrl('show', array('id' => $user->getId())));
}
}

extending sonata user bundle with more form fields, get Could not load type Application\Sonata\UserBundle\Form\RegistrationType

Nothing from above worked in Symfony 2.7 for me. I upgraded to Symfony 3.4 and it now works!! perfectly following the standard guide https://symfony.com/doc/master/bundles/FOSUserBundle/overriding_forms.html an upgrade really was needed, probably the autowiring features helped.

Add fields for Sonata UserBundle

Did you run php bin/console make:migration after adjusting the Entity?

  • php bin/console make:entity (create or update the Entity)
  • If you prefer to add new properties manually, the make:entity command can generate the getter & setter methods for you: php bin/console make:entity --regenerate
  • php bin/console make:migration
  • inspect src/Migrations/ folder
  • run the migrations php bin/console doctrine:migrations:migrate

read

Extending the User entity

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



Related Topics



Leave a reply



Submit