Extend Entities¶
Sulu has a very easy way to extend and replace the internal entities. This feature is not implemented for each entity but it will be implemented for all soon.
These entities are ready to extend:
- User
- Role
- Contact
- Media
You can extend all of them in the same way. Therefor we explain it for User here.
Create a Entity¶
Create your own Entity for example in the ClientWebsiteBundle. You can use the doctrine:generate:entity command for that. Extend the generated Entity with the Sulu User class.
<?php
namespace Client\Bundle\WebsiteBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Sulu\Bundle\SecurityBundle\Entity\User as SuluUser;
/**
* The following annotations are required for replacing the table of the extended entity:
*
* @ORM\Table(name="se_users")
* @ORM\Entity
*/
class User extends SuluUser
{
/**
* @var string
*
* @ORM\Column(name="myProperty", type="string", length=255, nullable = true)
*/
private $myProperty;
/**
* Set myProperty
*
* @param string $myProperty
* @return User
*/
public function setMyProperty($myProperty)
{
$this->myProperty = $myProperty;
return $this;
}
/**
* Get myProperty
*
* @return string
*/
public function getMyProperty()
{
return $this->myProperty;
}
}
Warning
Your Entity can have own properties, but they should have at least default values. Otherwise the normal features of Sulu could crash (like the sulu:security:user:create command).
Warning
The @ORM\Table annotation on your entity must match the table of the extended entity. Otherwise, doctrine might run into errors when querying data of the entity.
Configuration¶
You can specify your new Entity and if it exists your Repository in the sulu_security configuration section in the file app/config/config.yml.
For the User entity (se_users):
sulu_security:
objects:
user:
model: Client\Bundle\WebsiteBundle\Entity\User
repository: Sulu\Bundle\SecurityBundle\Entity\UserRepository
For the Role entity (se_roles):
sulu_security:
objects:
role:
model: Sulu\Bundle\SecurityBundle\Entity\Role
repository: Sulu\Bundle\SecurityBundle\Entity\RoleRepository
For the Contact entity (co_contacts):
sulu_contact:
objects:
contact:
model: Sulu\Bundle\ContactBundle\Entity\Contact
repository: Sulu\Bundle\ContactBundle\Entity\ContactRepository
For the Media entity (me_media):
sulu_media:
objects:
media:
model: Sulu\Bundle\MediaBundle\Entity\Media
repository: Sulu\Bundle\MediaBundle\Entity\MediaRepository
Warning
If you override entities in an existing project, you need to migrate the existing data to avoid data loss.