Annotations

All available annotations for Extbase are placed within the namespace TYPO3\CMS\Extbase\Annotation.

Example in the blog example for the annotation Lazy:

EXT:blog_example/Classes/Domain/Model/Post.php
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3\CMS\Extbase\Annotation\ORM\Lazy;

class Post extends AbstractEntity
{
    /**
     * @var ObjectStorage<Post>
     * @Lazy
     */
    public ObjectStorage $relatedPosts;
}

Annotations provided by Extbase

The following annotations are provided Extbase:

Validate

@TYPO3\CMS\Extbase\Annotation\Validate: Allows to configure validators for properties and method arguments. See Validation for details.

Can be used in the context of a model.

Example:

EXT:blog_example/Classes/Domain/Model/Blog.php
use TYPO3\CMS\Extbase\Annotation\Validate;

class Blog extends AbstractEntity
{
    /**
     * A short description of the blog
     *
     * @Validate("StringLength", options={"maximum": 150})
     */
    public string $description = '';
}

IgnoreValidation

@TYPO3\CMS\Extbase\Annotation\IgnoreValidation(): Allows to ignore Extbase default validation for a given argument.

Used in context of a controller.

Example:

EXT:blog_example/Classes/Controller/BlogController.php
use FriendsOfTYPO3\BlogExample\Domain\Model\Blog;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Extbase\Annotation\IgnoreValidation;

class BlogController extends AbstractController
{
    /**
     * Displays a form for creating a new blog
     *
     * $newBlog is taken as a basis for the rendering
     *
     * @IgnoreValidation("newBlog")
     */
    public function newAction(Blog $newBlog = null): ResponseInterface
    {
        $this->view->assign('newBlog', $newBlog);
        $this->view->assign('administrators',
            $this->administratorRepository->findAll());
        return $this->htmlResponse();
    }
}

ORM (object relational model) annotations

The following annotations can only be used on models:

Cascade

@TYPO3\CMS\Extbase\Annotation\ORM\Cascade("remove"): Allows to remove child entities during deletion of aggregate root.

Extbase only supports the option "remove".

Example:

EXT:blog_example/Classes/Domain/Model/Blog.php
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3\CMS\Extbase\Annotation\ORM\Lazy;
use TYPO3\CMS\Extbase\Annotation\ORM\Cascade;

class Blog extends AbstractEntity
{
    /**
     * The posts of this blog
     *
     * @var ObjectStorage<Post>
     * @Lazy
     * @Cascade("remove")
     */
    public $posts;
}

Transient

@TYPO3\CMS\Extbase\Annotation\ORM\Transient: Marks property as transient (not persisted).

Example:

EXT:blog_example/Classes/Domain/Model/Person.php
use TYPO3\CMS\Extbase\Annotation\ORM\Transient;

class Person extends AbstractEntity
{
    /**
     * @Transient
     */
    protected string $fullname = '';
}

Lazy

@TYPO3\CMS\Extbase\Annotation\ORM\Lazy: Marks property to be lazily loaded on first access.

Note

Lazy loading can greatly improve the performance of your actions.

Example:

EXT:blog_example/Classes/Domain/Model/Post.php
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3\CMS\Extbase\Annotation\ORM\Lazy;

class Post extends AbstractEntity
{
    /**
     * @var ObjectStorage<Post>
     * @Lazy
     */
    public ObjectStorage $relatedPosts;
}

Combining annotations

Annotations can be combined. For example Lazy loading and removal on cascade are frequently combined:

EXT:blog_example/Classes/Domain/Model/Post.php
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3\CMS\Extbase\Annotation\ORM\Lazy;
use TYPO3\CMS\Extbase\Annotation\ORM\Cascade;

class Post extends AbstractEntity
{
    /**
     * @var ObjectStorage<Comment>
     * @Lazy
     * @Cascade("remove")
     */
    public ObjectStorage $comments;
}

Several validations can also be combined. See Validation for details.