繁体   English   中英

检查数据库中是否存在值

[英]Check if value exists in the database

我想检查表中是否存在$_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.';
}

在这种情况下,用户总是会被注册。

我的代码有什么问题?

使用方法findBy()获得关联数组“ field” =>“ value”`作为参数。

因此,在您的示例中(假设电子邮件存储在名为email列中):

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

find($ id)

findBy(array())

findOneBy(array())

我认为您不应混淆这三种方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM