Php 如何检查find或findBy是否返回Null?
我有两个实体Php 如何检查find或findBy是否返回Null?,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我有两个实体 客户端 Info 使用一对一单向关系,作为客户端可以没有或只有一个信息 我正在尝试检查客户端是否已经有信息: $em = $this->getDoctrine()->getManager(); $check = $em->getRepository("MyBundle:Info")>findBy(array( 'client_id' => $id, )); 请注意,$id将是我已经作为参数传递并有权访问的客户端id。问题是,$chec
客户端
Info
客户端
可以没有或只有一个信息
我正在尝试检查客户端是否已经有信息
:
$em = $this->getDoctrine()->getManager();
$check = $em->getRepository("MyBundle:Info")>findBy(array(
'client_id' => $id,
));
请注意,$id
将是我已经作为参数传递并有权访问的客户端id。问题是,$check
将是什么类型的数据,以便我能够验证它,如下所示:
if ($check ??..) {
//..do this
} else {
//.. do that
}
就这么简单。使用findOneBy()
,如果找不到实体,则返回实体或null
:
$em = $this->getDoctrine()->getManager();
$info = $em->getRepository("MyBundle:Info")->findOneBy([
'client_id' => $id,
]);
if ($info) {
// manipulate existing info
} else {
// create new info
}
有关参考,请参阅:
就是这么简单。使用
findOneBy()
,如果找不到实体,则返回实体或null
:
$em = $this->getDoctrine()->getManager();
$info = $em->getRepository("MyBundle:Info")->findOneBy([
'client_id' => $id,
]);
if ($info) {
// manipulate existing info
} else {
// create new info
}
有关参考,请参阅: