Мне нужно проверить, существует ли объект Email
в ArrayCollection
, но я должен выполнить проверку по электронной почте как строки (Entity содержит идентификатор и некоторые отношения с другими лицами, по этой причине я использую отдельную таблицу, которая сохраняет все электронные письма).Как использовать метод 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;
}
}
Но чтение ArrayCollection
класс я видел метод exists
что, кажется, более elgant способ сделать то же самое, что я сделал.
Но я не знаю, как его использовать: может кто-нибудь объяснить мне, как использовать этот метод с учетом кода выше?
привет @Aerendir хорошая работа! – Matteo