Php Doctrine2-通过示例进行查询

Php Doctrine2-通过示例进行查询,php,doctrine-orm,Php,Doctrine Orm,我对第二条教义很陌生。我想通过示例进行查询—就像在Hibernate中一样。基本上,您将部分填充的entitiy对象作为查询参数,ORM返回所有匹配的对象。详情如下: 有这样的功能吗?到目前为止,我所发现的只是能够将包含id字段的实体作为参数,但其他字段呢?您可以通过多种方式进行查询,而不仅仅是基于id 如果您需要基于first_name&last_name获取条令对象,您可以这样做 $em = $this->getDoctrine()->getManager(); $em->

我对第二条教义很陌生。我想通过示例进行查询—就像在Hibernate中一样。基本上,您将部分填充的entitiy对象作为查询参数,ORM返回所有匹配的对象。详情如下:


有这样的功能吗?到目前为止,我所发现的只是能够将包含id字段的实体作为参数,但其他字段呢?

您可以通过多种方式进行查询,而不仅仅是基于id

如果您需要基于first_name&last_name获取条令对象,您可以这样做

$em = $this->getDoctrine()->getManager();
$em->getRepository('ACMETestBundle:Person')->findBy(array('first_name' => 'david', 'last_name' => 'john'));
$em = $this->getDoctrine()->getManager();
$em->getRepository('ACMETestBundle:Person')->findOneBy(array('first_name' => 'david', 'last_name' => 'john'));
如果您需要根据相同的名字和姓氏找到一个结果,它将是这样的

$em = $this->getDoctrine()->getManager();
$em->getRepository('ACMETestBundle:Person')->findBy(array('first_name' => 'david', 'last_name' => 'john'));
$em = $this->getDoctrine()->getManager();
$em->getRepository('ACMETestBundle:Person')->findOneBy(array('first_name' => 'david', 'last_name' => 'john'));
如果需要查询并获取数组。您可以使用以下选项

$em = $this->getDoctrine()->getManager();
$result = $em->createQueryBuilder();
$contacts = $result->select('p')
            ->from('ACMETestBundle:Person', 'p')
            ->where('p.first_name = :fName')
            ->andWhere('p.last_name = :lName')
            ->setParameter('fname', 'david')
            ->setParameter('lname', 'john')
            ->getQuery()
            ->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
因此,基本上有很多方法/选项可以循环查询,而不仅仅是限制id

希望这对你有帮助


干杯

您的意思是基于id以外的字段通过实体进行查询吗?