2017-02-11 34 views
-1

我想檢查我的表中是否存在$_POST['email']的值。如果不是,則應將新值添加到數據庫中。檢查數據庫中是否存在值

$member = new Members(); 
$member->setEmail($_POST['email']); 
$member->setName($_POST['vorname']); 
$member->setPassword(password_hash($_POST['pw1'], PASSWORD_DEFAULT)); 

$em = $this->getDoctrine()->getManager(); 
$em->persist($member); 

$get_email = $this->getDoctrine()->getRepository('AppBundle:Members')->find($_POST['email']); 

if (!$get_email) { 
    $em->flush(); 
    echo 'User registered.'; 
} 
else { 
    echo 'User already exists.'; 
} 

在這種情況下,用戶總是得到註冊。

我的代碼有什麼問題?

回答

3

使用方法findBy()獲取關聯數組「field」=>「value」`作爲參數。

在您例如

所以(假設電子郵件存儲在一個名爲email列):

$get_email = $this->getDoctrine()->getRepository('AppBundle:Members')->findBy(array('email' => $_POST['email'])); 
+2

是不是'findBy()'而是find()',當你傳遞一個這樣的數組? –

+0

@MagnusEriksson你是絕對正確的。 'findBy'對我來說就像魅力! –

+0

哦,對不起,我錯過了OP的帖子。更正後,感謝@MagnusEriksson的通知 –

0

查找($ ID)

findBy(陣列())

findOneBy(陣列() )

我認爲你不應該混淆這三種方法

相關問題