Email
エンティティが既にArrayCollection
に存在するかどうかをチェックする必要がありますが、電子メールのチェックを文字列として実行する必要があります(EntityにはIDと他のエンティティ、このため、私はすべての電子メールを保持する別のテーブルを使用します)。DoctrineのArrayCollection :: existsメソッドの使用方法
/**
* A new Email is adding: check if it already exists.
*
* In a normal scenario we should use $this->emails->contains().
* But it is possible the email comes from the setPrimaryEmail method.
* In this case, the object is created from scratch and so it is possible it contains a string email that is
* already present but that is not recognizable as the Email object that contains it is created from scratch.
*
* So we hav to compare Email by Email the string value to check if it already exists: if it exists, then we use
* the already present Email object, instead we can persist the new one securely.
*
* @var Email $existentEmail
*/
foreach ($this->emails as $existentEmail) {
if ($existentEmail->getEmail()->getEmail() === $email->getEmail()) {
// If the two email compared as strings are equals, set the passed email as the already existent one.
$email = $existentEmail;
}
}
しかし、私は私は同じことをやって、よりelgantな方法であると思われる方法exists
を見ArrayCollection
クラスを読み込む:
は今、最初に私はこのコードを書きました。
しかし、私はそれを使用する方法がわかりません:上記のコードでこのメソッドを使用する方法を誰かに教えてもらえますか?
ハイテク良い仕事@Aerendir:
ただ、完全を期すために、これは私が来たとのコードです! – Matteo