Skip to content

Commit 718599e

Browse files
author
Aurélien OUDART
committed
Add documentation for identifierFieldNames option
1 parent 2bbf70e commit 718599e

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

reference/constraints/UniqueEntity.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,59 @@ each with a single field.
274274

275275
.. include:: /reference/constraints/_groups-option.rst.inc
276276

277+
``identifierFieldNames``
278+
~~~~~~~~~~~~~~~~~~~~~~~~
279+
280+
**type**: ``array`` **default**: ``null``
281+
282+
When updating an entity through a PHP class (e.g. DTOs), this option can be used to
283+
define the class properties that are used as the Doctrine entity key (or composite key).
284+
285+
Consider this Doctrine entity ::
286+
287+
// src/Entity/User.php
288+
namespace App\Entity;
289+
290+
use Doctrine\ORM\Mapping as ORM;
291+
292+
#[ORM\Entity]
293+
class User
294+
{
295+
#[ORM\Id]
296+
#[ORM\GeneratedValue]
297+
#[ORM\Column]
298+
private int $id;
299+
300+
#[ORM\Column]
301+
private string $name;
302+
}
303+
304+
// ... getter and setter methods
305+
306+
For exemple, in a :doc:`Messenger component </components/messenger>` message that
307+
updates ``User`` entities, you can check its uniqueness by defining its identifier::
308+
309+
// src/Message/UpdateEmployeeProfile
310+
namespace App\Message;
311+
312+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
313+
314+
#[UniqueEntity(
315+
fields: ['name'],
316+
entityClass: User::class,
317+
// 'uid' is the property name in the PHP class and 'id' is the name of
318+
// the Doctrine entity property used as the primary key
319+
identifierFieldNames: ['uid' => 'id'],
320+
)]
321+
class UpdateEmployeeProfile
322+
{
323+
public function __construct(
324+
private string $uid,
325+
private string $name,
326+
) {
327+
}
328+
}
329+
277330
``ignoreNull``
278331
~~~~~~~~~~~~~~
279332

0 commit comments

Comments
 (0)