オリジナルを保持したい場合は、一意のフィールドが以前に使用されていないことを確認する方法を見つける必要があります。これを行う最も簡単な方法は、ユーザー フィールドにカスタム リポジトリを使用し、検索の前に softdeleteable フィルターを無効にすることだと思います。
デフォルトでは、 UniqueEntity は findBy とクラスに設定されたリポジトリを使用しますが、通常のメソッドをそのまま残しながら制約をいじる必要がないように、デフォルトでフィルターを無効にして独自のメソッドを作成することは理にかなっています。
FOSUserBundle を使用しているため (またはテーブル名からそう思われるfos_user
)、マッピングに repositoryClass を設定できます (XML は非常に複雑ですが、ここで確認できます)...
// Annotation
@ORM\Entity(repositoryClass="Acme\StoreBundle\Entity\ProductRepository")
//YAML
Acme\UserBundle\Entity\User:
type: entity
repositoryClass: Acme\UserBundle\Entity\UserRepository
そして、UserRepository
あなたのメソッドを追加するだけでfindIncludingSoftdeletedBy
、softdeleteableフィルターを無効にします...
namespace Acme\UserBundle\Entity;
use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository
{
/**
* Finds users by a set of criteria including sofdeleted.
*
* @param array $criteria
* @param array|null $orderBy
* @param int|null $limit
* @param int|null $offset
*
* @return array The objects.
*/
public function findIncludingSoftdeletedBy(
array $criteria, array $orderBy = null, $limit = null, $offset = null
)
{
// Get array of enabled filters
$enabledFilters = $this->em->getFilters()->getEnabledFilters();
// If softdeleteable (or soft-deleteable depending on config)
// is in array of enabled filters disable it
if (array_key_exists('softdeleteable', $endabledFilters)) {
// disabled softdeleteable filter ($this->em being entity manager)
$this->_em->getFilters()->disable('softdeleteable');
}
// return regular "findBy" including softdeleted users
return $this->findBy($criteria, $orderBy, $limit, $offset);
}
}
アップデート
このビットを忘れていました。
次に、この新しい検証制約を参照する独自の検証ファイルを作成する必要があります。(FOSUserBundle と YAML の場合 (私は YAML の方が好きです。XML は画面上で物理学の本が病気になっているように見えます))。
Acme\UserBundle\Entity\User:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: usernameCanonical
errorPath: username
message: fos_user.username.already_used
// Your method instead of the default "findBy"
method: findIncludingSoftdeletedBy
groups: [ Registration, Profile ]
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: emailCanonical
errorPath: email
message: fos_user.email.already_used
// Your method instead of the default "findBy"
method: findIncludingSoftdeletedBy
groups: [ Registration, Profile ]
UniqueEntity 制約の詳細については、具体的にはドキュメントを参照してください。
フィールド
タイプ: 配列 | 文字列 [デフォルト オプション]
この必須オプションは、このエンティティが一意である必要があるフィールド (またはフィールドのリスト) です。たとえば、1 つの UniqueEntity 制約で電子メール フィールドと名前フィールドの両方を指定した場合、組み合わせ値が一意であることが強制されます (たとえば、2 人のユーザーが同じ名前を持っていない限り、同じ電子メール アドレスを持つことができます)。 )。
2 つのフィールドを個別に一意にする必要がある場合 (たとえば、一意の電子メールと一意のユーザー名)、2 つの UniqueEntity エントリを使用し、それぞれに 1 つのフィールドを指定します。