Annotations

All available annotations for Extbase delivered by TYPO3 Core 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\Annotation\ORM\Lazy;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;

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

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 property.

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 = '';
}
Copied!

IgnoreValidation

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

Used in context of a controller action.

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
     *
     * @IgnoreValidation("newBlog")
     */
    public function newAction(?Blog $newBlog = null): ResponseInterface
    {
        $this->view->assign('newBlog', $newBlog);
        $this->view->assign(
            'administrators',
            $this->administratorRepository->findAll()
        );
        return $this->htmlResponse();
    }
}
Copied!

ORM (object relational model) annotations

The following annotations can only be used on model properties:

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\Annotation\ORM\Cascade;
use TYPO3\CMS\Extbase\Annotation\ORM\Lazy;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;

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

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 = '';
}
Copied!

Lazy

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

Example:

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

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

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\Annotation\ORM\Cascade;
use TYPO3\CMS\Extbase\Annotation\ORM\Lazy;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;

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

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