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
  • Account
  • Category
  • Media
  • Tag

You can extend all of them in the same way. Therefore we explain it for User here.

Create a Entity

Create your own Entity for example in the src folder and extends the Entity with the Sulu User class.

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Sulu\Bundle\SecurityBundle\Entity\User as SuluUser;

/**
 * User
 *
 * @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).

Configuration

You can specify your new entity and if it exists your repository in the configuration section of the file config/packages/*. If the file does not exist you need to create it.

For the User entity:

# config/packages/sulu_security.yaml
sulu_security:
    objects:
        user:
            model: App\Entity\User
            repository: Sulu\Bundle\SecurityBundle\Entity\UserRepository

For the Role entity:

# config/packages/sulu_security.yaml
sulu_security:
    objects:
        role:
            model:                Sulu\Bundle\SecurityBundle\Entity\Role
            repository:           Sulu\Bundle\SecurityBundle\Entity\RoleRepository

For the Contact entity:

# config/packages/sulu_contact.yaml
sulu_contact:
    objects:
        contact:
            model:                Sulu\Bundle\ContactBundle\Entity\Contact
            repository:           Sulu\Bundle\ContactBundle\Entity\ContactRepository

For the Account entity:

# config/packages/sulu_contact.yaml
sulu_contact:
    objects:
        account:
            model:                Sulu\Bundle\ContactBundle\Entity\Account
            repository:           Sulu\Bundle\ContactBundle\Entity\AccountRepository

For the Category entity:

# config/packages/sulu_category.yaml
sulu_category:
    objects:
        category:
            model:                Sulu\Bundle\CategoryBundle\Entity\Category
            repository:           Sulu\Bundle\CategoryBundle\Entity\CategoryRepository
        category_meta:
            model:                Sulu\Bundle\CategoryBundle\Entity\CategoryMeta
            repository:           Sulu\Bundle\CategoryBundle\Entity\CategoryMetaRepository
        category_translation:
            model:                Sulu\Bundle\CategoryBundle\Entity\CategoryTranslation
            repository:           Sulu\Bundle\CategoryBundle\Entity\CategoryTranslationRepository
        keyword:
            model:                Sulu\Bundle\CategoryBundle\Entity\Keyword
            repository:           Sulu\Bundle\CategoryBundle\Entity\KeywordRepository

For the Media entity:

# config/packages/sulu_media.yaml
sulu_media:
    objects:
        media:
            model:                Sulu\Bundle\MediaBundle\Entity\Media
            repository:           Sulu\Bundle\MediaBundle\Entity\MediaRepository

For the Tag entity:

# config/packages/sulu_tag.yaml
sulu_tag:
    objects:
        tag:
            model:                Sulu\Bundle\TagBundle\Entity\Tag
            repository:           Sulu\Bundle\TagBundle\Entity\TagRepository

Warning

If you override the entities you lose your old tables and data. You should provide a upgrade script.