diff --git a/doc/annotations.md b/doc/annotations.md index 49c9014aa..75a881086 100644 --- a/doc/annotations.md +++ b/doc/annotations.md @@ -1,567 +1,1505 @@ -# Annotation reference +# Annotations Reference -Bellow you will find all annotation descriptions used in these extensions. -There will be introduction on usage with examples. For more detailed usage -on extensions, refer to their specific documentation. +> [!IMPORTANT] +> To use annotations, you will need the deprecated [`doctrine/annotations`](https://www.doctrine-project.org/projects/annotations.html) library. PHP 8 users are encouraged to migrate to attributes as annotation support is deprecated in all supported Doctrine object managers. -Content: +Below you will a reference for annotations supported in this extensions library. +There will be introduction on usage with examples. For more detailed usage of each +extension, refer to the extension's documentation page. -- Best [practices](#em-setup) for setting up -- [Tree](#gedmo-tree) -- [Translatable](#gedmo-translatable) -- [Sluggable](#gedmo-sluggable) -- [Timestampable](#gedmo-timestampable) -- [Loggable](#gedmo-loggable) +## Index -## Annotation mapping +- [Blameable Extension](#blameable-extension) +- [IP Traceable Extension](#ip-traceable-extension) +- [Loggable Extension](#loggable-extension) +- [Reference Integrity Extension](#reference-integrity-extension) +- [References Extension](#references-extension) +- [Sluggable Extension](#sluggable-extension) +- [Soft Deleteable Extension](#soft-deleteable-extension) +- [Sortable Extension](#sortable-extension) +- [Timestampable Extension](#timestampable-extension) +- [Translatable Extension](#translatable-extension) +- [Tree Extension](#tree-extension) +- [Uploadable Extension](#uploadable-extension) -Starting from **doctrine2.1.x** versions you have to import all used annotations -by an **use** statement, see example bellow: +## Reference + +### Blameable Extension + +The below annotations are used to configure the [Blameable extension](./blameable.md). + +#### `@Gedmo\Mapping\Annotation\Blameable` + +The `Blameable` annotation is a property annotation used to identify fields which are updated to show information +about the last user to update the mapped object. A blameable field may have either a string value or a one-to-many +relationship with another entity. + +Required Attributes: + +- **on** - By default, the annotation configures the property to be updated when an object is updated; + this can be set to one of \[`change`, `create`, `update`\] + +Optional Attributes: + +- **field** - An optional list of properties to limit updates to the blameable field; this option is only + used when the **on** option is set to "change" and can be a dot separated path to indicate + properties on a related object are watched (i.e. `user.email` to reference the `$email` property + of the `$user` relation on this object) + +- **value** - An optional value to require the configured **field** to match to update the blameable field; + this option is only used when the **on** option is set to "change" + +> [!WARNING] +> When both the **field** and **value** options are set, the **field** can only be set to a single field; checking the value against multiple fields is not supported at this time + +Examples: + +```php + [!WARNING] +> When both the **field** and **value** options are set, the **field** can only be set to a single field; checking the value against multiple fields is not supported at this time + +Examples: + +```php + [!WARNING] +> This extension is only usable with the Doctrine MongoDB ODM + +#### `@Gedmo\Mapping\Annotation\ReferenceIntegrity` + +The `ReferenceIntegrity` annotation is a property annotation used to identify fields where referential integrity +should be checked. The annotation must be used on a property which references another document, and the reference +configuration must have a `mappedBy` configuration. + +Required Attributes: + +- **value** - The type of action to take for the reference, must be one of \[`nullify`, `pull`, `restrict`\] + +Example: + +```php + + * + * @ODM\ReferenceMany(targetDocument="App\Document\Comment", mappedBy="article") + * @Gedmo\ReferenceIntegrity("nullify") + */ + private Collection $comments; + + public function __construct() + { + $this->comments = new ArrayCollection(); + } +} +``` + +### References Extension + +The below annotations are used to configure the [References extension](./references.md). + +#### `@Gedmo\Mapping\Annotation\ReferenceOne` + +The `ReferenceOne` annotation is a property annotation used to create a reference between two objects in different +databases or object managers. This is similar to a `ReferenceOne` relationship in the MongoDB ODM. + +Required Attributes: + +- **value** - The type of action to take for the reference, must be one of \[`nullify`, `pull`, `restrict`\] + +- **type** - The type of object manager to use for the reference, must be one of \[`document`, `entity`\] + +- **class** - The class name of the object to reference + +Optional Attributes: + +- **identifier** - The name of the property to store the identifier value in + +- **inversedBy** - The name of the property on the inverse side of the reference + +Example: + +```php + + * + * @Gedmo\ReferenceMany(type="entity", class="App\Entity\Comment", mappedBy="comments") + */ + private Collection $comments; + + public function __construct() + { + $this->comments = new ArrayCollection(); + } +} +``` + +#### `@Gedmo\Mapping\Annotation\ReferenceMany` + +The `ReferenceMany` annotation is a property annotation used to create a reference between two objects in different +databases or object managers. This is similar to a `ReferenceMany` relationship in the MongoDB ODM. + +Required Attributes: + +- **value** - The type of action to take for the reference, must be one of \[`nullify`, `pull`, `restrict`\] + +- **type** - The type of object manager to use for the reference, must be one of \[`document`, `entity`\] + +- **class** - The class name of the object to reference + +Optional Attributes: + +- **identifier** - The name of the property to store the identifier value in + +Example: + +```php + + * + * @Gedmo\ReferenceManyEmbed(type="entity", class="App\Entity\Comment", identifier="metadata.commentId") + */ + private Collection $comments; + + public function __construct() + { + $this->comments = new ArrayCollection(); + } +} +``` + +### Sluggable Extension + +The below annotations are used to configure the [Sluggable extension](./sluggable.md). + +#### `@Gedmo\Mapping\Annotation\Slug` + +The `Slug` annotation is a property annotation used to identify the field the slug is stored to. + +Required Attributes: + +- **fields** - A list of fields on the object to use for generating a slug, this must be a non-empty list of strings + +Optional Attributes: + +- **updatable** - Flag indicating the slug can be automatically updated if any of the fields have changed, + defaults to `true` + +- **style** - The style to use while generating the slug, defaults to `default` (no style changes) and ignores + unsupported styles; supported styles are: + - `camel` - Converts the slug to a camel-case string + - `lower` - Converts the slug to a fully lowercased string + - `upper` - Converts the slug to a fully uppercased string + +- **unique** - Flag indicating the slug must be unique, defaults to `true` + +- **unique_base** - The name of the object property that should be used as a key when doing a uniqueness check, + can only be set when the **unique** flag is `true` + +- **separator** - The separator to use between words in the slug, defaults to `-` + +- **prefix** - An optional prefix for the generated slug + +- **suffix** - An optional suffix for the generated slug + +- **handlers** - A list of `@Gedmo\Mapping\Annotation\SlugHandler` annotations used to further customize the slug + generator behavior + +Basic Example: + +```php + - -## Best practices for setting up with annotations - -New annotation reader does not depend on any namespaces, for that reason you can use -single reader instance for whole project. The example bellow shows how to setup the -mapping and listeners: - -**Note:** using this repository you can test and check the [example demo configuration](../example/em.php) - -```php -addDriver($annotationDriver, 'Entity'); +### Sortable Extension + +The below annotations are used to configure the [Sortable extension](./sortable.md). + +#### `@Gedmo\Mapping\Annotation\SortableGroup` + +The `SortableGroup` annotation is a property annotation used to identify fields which are used to group objects of +this type for sorting. + +Example: + +```php +setProxyDir(sys_get_temp_dir()); -$config->setProxyNamespace('Proxy'); -$config->setAutoGenerateProxyClasses(false); // this can be based on production config. -// register metadata driver -$config->setMetadataDriverImpl($driverChain); -// use our already initialized cache driver -$config->setMetadataCacheImpl($cache); -$config->setQueryCacheImpl($cache); +/** + * @ORM\Entity + */ +class Article +{ + /** + * @ORM\Id + * @ORM\GeneratedValue + * @ORM\Column(type="integer") + */ + public ?int $id = null; -// create event manager and hook preferred extension listeners -$evm = new Doctrine\Common\EventManager(); -// gedmo extension listeners, remove which are not used + /** + * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="articles") + * @Gedmo\SortableGroup + */ + public ?Category $category = null; +} +``` -// sluggable -$sluggableListener = new Gedmo\Sluggable\SluggableListener; -// you should set the used annotation reader to listener, to avoid creating new one for mapping drivers -$sluggableListener->setAnnotationReader($cachedAnnotationReader); -$evm->addEventSubscriber($sluggableListener); +#### `@Gedmo\Mapping\Annotation\SortablePosition` -// tree -$treeListener = new Gedmo\Tree\TreeListener; -$treeListener->setAnnotationReader($cachedAnnotationReader); -$evm->addEventSubscriber($treeListener); +The `SortablePosition` annotation is a property annotation used to identify the field where the sorted position +(optionally within a group) is stored for the current object type. This field must be an integer field type. -// loggable, not used in example -$loggableListener = new Gedmo\Loggable\LoggableListener; -$loggableListener->setAnnotationReader($cachedAnnotationReader); -$evm->addEventSubscriber($loggableListener); +Example: -// timestampable -$timestampableListener = new Gedmo\Timestampable\TimestampableListener; -$timestampableListener->setAnnotationReader($cachedAnnotationReader); -$evm->addEventSubscriber($timestampableListener); +```php +setTranslatableLocale('en'); -$translatableListener->setDefaultLocale('en'); -$translatableListener->setAnnotationReader($cachedAnnotationReader); -$evm->addEventSubscriber($translatableListener); +use Doctrine\ORM\Mapping as ORM; +use Gedmo\Mapping\Annotation as Gedmo; -// sortable, not used in example -$sortableListener = new Gedmo\Sortable\SortableListener; -$sortableListener->setAnnotationReader($cachedAnnotationReader); -$evm->addEventSubscriber($sortableListener); +/** + * @ORM\Entity + */ +class Article +{ + /** + * @ORM\Id + * @ORM\GeneratedValue + * @ORM\Column(type="integer") + */ + public ?int $id = null; -// mysql set names UTF-8 if required -$evm->addEventSubscriber(new Doctrine\DBAL\Event\Listeners\MysqlSessionInit()); -// DBAL connection -$driverParams = array( - 'driver' => 'pdo_mysql', - 'host' => '127.0.0.1', - 'dbname' => 'test', - 'user' => 'root', - 'password' => '' -); -// Finally, create entity manager -$connection = DriverManager::getConnection($driverParams, $config); -$em = new EntityManager($connection, $config, $evm); + /** + * @ORM\Column(type="integer") + * @Gedmo\SortablePosition + */ + public ?int $position = null; +} ``` -**Note:** that Symfony StofDoctrineExtensionsBundle does it automatically this -way you will maintain a single instance of annotation reader. It relates only -to doctrine-common-2.1.x branch and newer. +### Timestampable Extension + +The below annotations are used to configure the [Timestampable extension](./timestampable.md). - +#### `@Gedmo\Mapping\Annotation\Timestampable` -## Tree annotations +The `Timestampable` annotation is a property annotation used to identify fields which are updated to record the +timestamp of the update the mapped object. A timestampable field must be a field supporting a `DateTimeInterface`. -Tree can use different adapters. Currently **Tree** extension supports **NestedSet** -and **Closure** strategies which has a difference for annotations used. Note, that -tree will automatically map indexes which are considered necessary for best performance. +Required Attributes: -### @Gedmo\Mapping\Annotation\Tree (required for all tree strategies) +- **on** - By default, the annotation configures the property to be updated when an object is updated; + this can be set to one of \[`change`, `create`, `update`\] -**class** annotation +Optional Attributes: -Is the main identificator of tree used for domain object which should **act as Tree**. +- **field** - An optional list of properties to limit updates to the timestampable field; this option is only + used when the **on** option is set to "change" and can be a dot separated path to indicate + properties on a related object are watched (i.e. `user.email` to reference the `$email` property + of the `$user` relation on this object) -**options:** +- **value** - An optional value to require the configured **field** to match to update the timestampable field; + this option is only used when the **on** option is set to "change" -- **type** - (string) _optional_ default: **nested** +> [!WARNING] +> When both the **field** and **value** options are set, the **field** can only be set to a single field; checking the value against multiple fields is not supported at this time -example: +Examples: ```php [!TIP] +> Although not strictly required, translation classes are encouraged to extend from the `AbstractPersonalTranslation` or `AbstractTranslation` classes in the `Gedmo\Translatable\\MappedSuperclass` namespace + +Example: ```php [!WARNING] +> Only the `materializedPath` tree type is supported for the MongoDB ODM at this time + +Optional Attributes: + +- **activateLocking** - Indicates that a materialized path tree should be locked during write transactions, + defaults to true + +- **lockingTimeout** - The time (in seconds) for the lock timeout, defaults to 3 + +Example: ```php +#### Materialized Path Strategy -## Translatable annotations +##### `@Gedmo\Mapping\Annotation\TreePath` -Translatable additionally can have unmapped property, which would override the -locale used by listener. +The `TreePath` annotation is a property annotation used to identify the property that the calculated path is stored in. -### @Gedmo\Mapping\Annotation\TranslationEntity (optional) +Optional Attributes: -**class** annotation +- **separator** - The separator used to separate path segments, defaults to `,` -This class annotation can force translatable to use **personal Entity** to store -translations. In large tables this can be very handy. +- **appendId** - Flag indicating the object ID should be appended to the computed path, defaults to `null` -**options:** +- **startsWithSeparator** - Flag indicating the path should begin with a separator, defaults to `false` -- **class** - (string) _required_ +- **endsWithSeparator** - Flag indicating the path should end with a separator, defaults to `true` -example: +Example: ```php +##### `@Gedmo\Mapping\Annotation\TreeLockTime` + +> [!WARNING] +> This annotation is only usable with the Doctrine MongoDB ODM + +The `TreeLockTime` annotation is a property annotation used to identify the property that the tree lock time is +stored in. This must be a date field. + +Example: -## Sluggable annotations +```php + +### Uploadable Extension + +The below annotations are used to configure the [Uploadable extension](./uploadable.md). -## Timestampable annotations +#### `@Gedmo\Mapping\Annotation\Uploadable` -Timestampable will update date fields on create, update or property change. If you set/force -date manually it will not update it. +The `Uploadable` annotation is a class annotation used to identify objects which can store information about +uploaded files. -### @Gedmo\Mapping\Annotation\Timestampable (required) +Optional Attributes: -**property** annotation +- **allowOverwrite** - Flag indicating that an existing uploaded file can be overwritten, defaults to `false` -Marks a **date, datetime or time** field as timestampable. +- **appendNumber** - Flag indicating that a number should be appended to the filename when the **allowOverwrite** + attribute is `true` and a file already exists, defaults to `false` -**options:** +- **path** - The file path to store files for this uploadable at; this must be configured unless the **pathMethod** + attribute is configured or a default path is set on the uploadable listener -- **on** - (string) _optional_ default: **update**, other choice is **create** or **change** -- **field** - (string) _conditional_ required only if it triggers on **change**, name of the **field** -or if it is a relation **property.field** -- **value** - (mixed) _conditional_ required only if it triggers on **change**, value of property -which would trigger an update. +- **pathMethod** - A method name on this class to use to retrieve the file path to store files for this uploadable; + this must be configured unless the **path** attribute is configured or a default path is set on + the uploadable listener -example: +- **callback** - A method name on this class to use to call after the file has been moved + +- **filenameGenerator** - A filename generator to use when moving the uploaded file to be used to normalize/customize + the file name and defaults to `NONE`; supported styles are: + - `ALPHANUMERIC` - Normalizes the filename, leaving only alphanumeric characters + - `NONE` - No conversion + - `SHA1` - Creates a SHA1 hash of the filename + - A class name of a class implementing `Gedmo\Uploadable\FilenameGenerator\FilenameGeneratorInterface` + +- **maxSize** - An optional maximum file size for this uploadable object; defaults to `0` + +- **allowedTypes** - An optional list of allowed MIME types for this uploadable object; + cannot be set at the same time as the **disallowedTypes** attribute + +- **disallowedTypes** - An optional list of disallowed MIME types for this uploadable object + cannot be set at the same time as the **allowedTypes** attribute + +Example: ```php +#### `@Gedmo\Mapping\Annotation\UploadableFileMimeType` -## Loggable annotations +The `UploadableFileMimeType` annotation is a property annotation used to identify the field that the uploadable's +MIME type is stored to. -Loggable is used to log all actions made on annotated object class, it logs insert, update -and remove actions for a username which currently is logged in for instance. -Further more, it stores all **Versioned** property changes in the log which allows -a version management implementation for this object. +Example: + +```php +