我想比較一個從表單輸入的電子郵件地址與數據庫中已存在的電子郵件地址,我認爲最好的方法是使用findByEmail方法。什麼是使用findBy'Field方法的正確方法?
我希望找到表中特定條目的電子郵件地址,但它會返回整個條目(名,姓和更多...)。 如何才能找到表格中條目的電子郵件地址?
我知道我可以使用foreach
遍歷條目,但我認爲這有點違背了使用findByEmail
函數的目的。
這裏是我試過到目前爲止:
$formEmail = $form->get('email')->getData();
$personEmail = $em->getRepository('UserBundle:User')->findByEmail($formEmail); //Should just find the email address of a person in the database.
var_dump($personsEmail); //returns the whole row associated with the email address (first name, last name…..)
var_dump(if($formEmail == $personEmail));die; //returns false when it should be true because it finds the whole row instead of the email address
實際上你試圖實現的目標並不明顯:如果找到了一行 - 它將使電子郵件與'$ formEmail'完全相同,因爲您將其用作搜索條件。 – zerkms
在這一行中:'if($ formEmail == $ personEmail)'你正在比較一個文本值和一個行值,這是行不通的! –
@AlvinBunk對不起,我在'foreach'循環中試圖將它遺留下來。 'foreach($ personEmails as $ email){$ personEmail = email-> getEmail(); var_dump($ formEmail == $ personEmail);}' – grgre