Created
September 17, 2012 18:41
How to extend the Sonata User Bundle's User
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
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.
@ureimers Thank you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you prefer annotations, you can do the following:
Overwrite the mapping type for your user class:
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 thephp app/console sonata:easy-extends:generate SonataUserBundle
command, where<dest-path>
is the optional destination path you gave that command using the--dest=
option):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 theGroup
entity left: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).