如果 uid 存在则更新一个值
Update a value if uid exists
我想更新 MySQL 数据库的值。
如果在数据库中找不到 uid
的用户,则应添加一个新行,其中包含 uid
和 firstname
的值,但如果 uid
存在于数据库中,只应更新列 first_name
的值。
这是我目前拥有的:
$em = $this->getDoctrine()->getManager();
$personal = new Personal();
$personal->setUid($uid);
$personal->setFirstName($value);
$em->persist($personal);
$em->flush();
这只会添加一个新行,但我该如何更新值?
您可以尝试这样的操作:
$em = $this->getDoctrine()->getManager();
$search = $this->getDoctrine()->getRepository(Personal::class)->findBy(array("uid" => $uid));
if (count($search) == 0) {
$personal = new Personal();
$personal->setUid($uid);
} else {
$personal = $search[0];
}
$personal->setFirstName($value);
$em->persist($personal);
$em->flush();
我想更新 MySQL 数据库的值。
如果在数据库中找不到 uid
的用户,则应添加一个新行,其中包含 uid
和 firstname
的值,但如果 uid
存在于数据库中,只应更新列 first_name
的值。
这是我目前拥有的:
$em = $this->getDoctrine()->getManager();
$personal = new Personal();
$personal->setUid($uid);
$personal->setFirstName($value);
$em->persist($personal);
$em->flush();
这只会添加一个新行,但我该如何更新值?
您可以尝试这样的操作:
$em = $this->getDoctrine()->getManager();
$search = $this->getDoctrine()->getRepository(Personal::class)->findBy(array("uid" => $uid));
if (count($search) == 0) {
$personal = new Personal();
$personal->setUid($uid);
} else {
$personal = $search[0];
}
$personal->setFirstName($value);
$em->persist($personal);
$em->flush();