Upgrade to Pro — share decks privately, control downloads, hide ads and more …

複合ユニーク制約について調べてみた Symfony Meetup Kansai #2 / Symfony Unique Constraints For Multiple Columns

shimokei53
August 07, 2019

複合ユニーク制約について調べてみた Symfony Meetup Kansai #2 / Symfony Unique Constraints For Multiple Columns

shimokei53

August 07, 2019
Tweet

Other Decks in Programming

Transcript

  1. user_id user_id team_id team_id (例) (例) 1 1 1 1

    - - 1 1 2 2 - - 2 2 2 2 - - 1 1 1 1 - - 2 2 . . 3 3
  2. 複数の複合ユニークもOK 複数の複合ユニークもOK /** /** * * @ORM @ORM\Entity() \Entity() *

    * @UniqueEntity @UniqueEntity(fields={"name", "email"}) (fields={"name", "email"}) * * @UniqueEntity @UniqueEntity(fields={"name", "team"}) (fields={"name", "team"}) */ */ class class User User 4 4 . . 4 4
  3. DBに変更なし DBに変更なし % bin/ % bin/console console doctrine:migrations:diff doctrine:migrations:diff In

    NoChangesDetected.php line In NoChangesDetected.php line 13 13: : No changes detected No changes detected in in your mapping information. your mapping information. 4 4 . . 5 5
  4. 別途Validatorが必要 別途Validatorが必要 form経由の登録とか form経由の登録とか Validator単独で使うとか Validator単独で使うとか class class UserService UserService

    { { private private $validator; $validator; public public function function __construct __construct(ValidatorInterface $validator (ValidatorInterface $validator { { $this $this->validator = $validator; ->validator = $validator; } } public public function function validateUser validateUser(User $user) (User $user) { { // ... // ... $errors = $errors = $this $this->validator->validate($user); ->validator->validate($user); if if (count($errors) > (count($errors) > 0 0) { ) { 4 4 . . 6 6
  5. 複数の複合ユニークもOK 複数の複合ユニークもOK /** /** * * @ORM @ORM\Entity() \Entity() *

    * @ORM @ORM\Table( \Table( * name="user", * name="user", * uniqueConstraints={ * uniqueConstraints={ * * @ORM @ORM\UniqueConstraint( \UniqueConstraint( * name="name_email_unique", * name="name_email_unique", * columns={"name", "email"} * columns={"name", "email"} * ), * ), * * @ORM @ORM\UniqueConstraint( \UniqueConstraint( * name="name_team_unique", * name="name_team_unique", * columns={"name", "team_id"} * columns={"name", "team_id"} * ), * ), * }, * }, * ) * ) 5 5 . . 4 4
  6. DBに変更が⼊る DBに変更が⼊る % % bin/console doctrine:migrations:diff bin/console doctrine:migrations:diff $this $this->addSql(

    ->addSql( 'CREATE UNIQUE INDEX name_email_unique ON user (name, email 'CREATE UNIQUE INDEX name_email_unique ON user (name, email ); ); $this $this->addSql( ->addSql( 'CREATE UNIQUE INDEX name_team_unique ON user (name, team_i 'CREATE UNIQUE INDEX name_team_unique ON user (name, team_i ); ); 5 5 . . 5 5
  7. 永続化しようとした時点で弾かれる 永続化しようとした時点で弾かれる Validator不要だが、try-catchしておきましょう Validator不要だが、try-catchしておきましょう $em->persist($user1); $em->persist($user1); // ... // ...

    $em->persist($user2); $em->persist($user2); // throw Doctrine\DBAL\Exception\UniqueConstraintViolationExc // throw Doctrine\DBAL\Exception\UniqueConstraintViolationExc $em->flush(); $em->flush(); 5 5 . . 6 6