如何检查 find 或 findBy 是否返回 Null?

Kha*_*ani 0 php symfony doctrine-orm

我有两个实体

  • Client
  • Info

具有一对一的单向关系,因为 aClient可以有 none 或 one Info

我正在尝试检查是否Client已经有了Info这个:

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

$check = $em->getRepository("MyBundle:Info")>findBy(array(
    'client_id' => $id, 
));
Run Code Online (Sandbox Code Playgroud)

请注意,这$id将是我已经作为参数传递并有权访问的客户端 ID。问题是什么样的数据会$check是这样我可以验证它如下所示:

if ($check ??..) {
    //..do this
} else { 
    //.. do that
}
Run Code Online (Sandbox Code Playgroud)

svg*_*fov 6

就是这么简单。使用findOneBy(),它返回一个实体,或者null如果找不到实体:

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

$info = $em->getRepository("MyBundle:Info")->findOneBy([
    'client_id' => $id,
]);

if ($info) {
    // manipulate existing info
} else {
    // create new info
}
Run Code Online (Sandbox Code Playgroud)

参考: