Skip to content

Instantly share code, notes, and snippets.

@jmather
Created September 17, 2012 18:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmather/3738985 to your computer and use it in GitHub Desktop.
Save jmather/3738985 to your computer and use it in GitHub Desktop.
How to extend the Sonata User Bundle's User
<!-- Application/Sonata/UserBundle/Resources/config/doctrine/User.orm.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Application\Sonata\UserBundle\Entity\User" table="fos_user_user">
<id name="id" column="id" type="integer">
<generator strategy="AUTO" />
</id>
<many-to-many field="organizations" target-entity="Your\Bundle\Entity\Organization" mapped-by="users" />
</entity>
</doctrine-mapping>
<?php
// Application/Sonata/UserBundle/Entity/User.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 ( http://sonata-project.org/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
{
protected $organizations;
public function __construct()
{
parent::__construct();
$this->organizations = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @var integer $id
*/
protected $id;
/**
* Get id
*
* @return integer $id
*/
public function getId()
{
return $this->id;
}
public function addOrganization($organization)
{
$this->organizations[] = $organization;
}
public function setOrganizations($organizations)
{
$this->organizations = $organizations;
}
public function getOrganizations()
{
return $this->organizations;
}
}
@ureimers
Copy link

If you prefer annotations, you can do the following:

Overwrite the mapping type for your user class:

# app/config.yml
doctrine:
    # ... #
    orm:
        # ... #
        mappings:
            ApplicationSonataUserBundle:
                type: annotation

Edit your user class to incorporate the mappings formally defined in <dest-path>/Application/Sonata/UserBundle/Resources/config/doctrine/User.orm.xml (the file created by the php app/console sonata:easy-extends:generate SonataUserBundle command, where <dest-path> is the optional destination path you gave that command using the --dest= option):

<?php
// <dest-path>/Application/Sonata/UserBundle/Entity/User.php
namespace Application\Sonata\UserBundle\Entity;

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

/**
 * This file has been generated by the Sonata EasyExtends bundle ( http://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
 *
 * @ORM\Entity
 * @ORM\Table(name="fos_user_user")
 *
 * @author <yourname> <youremail>
 */
class User extends BaseUser
{
    /**
     * @var integer $id
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

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

As time of this writing I don't know how to overwrite only one specific mapping so we need to define all mappings of the ApplicationSonataUserBundle as annotations. Which isn't that hard because there's only the Group entity left:

// <dest-path>/Application/Sonata/UserBundle/Entity/Group.php

<?php
namespace Application\Sonata\UserBundle\Entity;

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

/**
 * This file has been generated by the Sonata EasyExtends bundle ( http://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
 *
 * @ORM\Entity
 * @ORM\Table(name="fos_user_group")
 *
 * @author <yourname> <youremail>
 */
class Group extends BaseGroup
{
    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

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

You can then go on and add new fields. To test your mappings you can simply call app/console doctrine:mapping:info -vvv (the -vvv adds stack traces if exceptions occur which may help you debugging the problem).

@NelsonSR
Copy link

I've been trying to achieve almost the same process for days (in my case, two one-to-many relationships between emails and phones with user).

The most estrange thing is that works one time, with both forms embedded into User form, but when I try a second time it keeps loading forever the form or the list (I have to stop it with apache2ctl stop). It seems as something is missing, but I've read almost every single method (even this one) and it doesn't work as the other entities relationships in my project.

@yuri-kovalev
Copy link

@ureimers Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment