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

I have two entities

  • Client
  • Info

with a one-to-one-unidirectional relationship, as a Client can have none or one Info.

I am trying to check if Client already has Info with this:

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

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

Note that $id would be the client id I am already passing as a parameter and have access to. The question is what sort of data would $check would be so I can be able to verify it as shown below:

if ($check ??..) {
    //..do this
} else { 
    //.. do that
}

It is that simple. Use findOneBy(), which returns either an entity or null if the entity can not be found:

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

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

if ($info) {
    // manipulate existing info
} else {
    // create new info
}

For reference, see: