本文介绍了在原则中findByExample的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Doctrine 中是否有一种方法,例如 Hibernate findByExample 方法?

Is there a method in Doctrine like Hibernate's findByExample method?

谢谢

推荐答案

您可以使用 findBy 方法,它是继承的,并且存在于所有存储库中。

You can use the findBy method, which is inherited and is present in all repositories.

示例:

$criteria = array('name' => 'someValue', 'status' => 'enabled');
$result = $em->getRepository('SomeEntity')->findBy($criteria);

您可以在其中一个中创建 findByExample 您的存储库使用如下定义:

You can create findByExample method in one of your repositories using a definition like this:

class MyRepository extends Doctrine\ORM\EntityRepository {
    public function findByExample(MyEntity $entity) {
        return $this->findBy($entity->toArray());
    }
}

为了让这个工作,你必须为实体创建自己的基类,实现 toArray 方法。

In order for this to work, you will have to create your own base class for the entities, implementing the toArray method.

MyEntity 也可以是一个接口,您的特定实体将不得不再次实现 toArray 方法。

MyEntity can also be an interface, which your specific entities will have to implement the toArray method again.

要在所有的存储库中使用这个功能,请确保您扩展了基础存储库类 - 在此示例中, MyRepository 一个。

To make this available in all your repositories, ensure that you are extending your base repository class - in this example, the MyRepository one.

PS我假设你在谈论 Doctrine 2.x

P.S I assume you are talking about Doctrine 2.x

这篇关于在原则中findByExample的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 16:55