教义中的 findByExample [英] findByExample in Doctrine

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

问题描述

Doctrine中是否有类似HibernatefindByExample方法的方法?

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 DoctrineORMEntityRepository {
    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.

P.S 我假设你在谈论 Doctrine 2.x

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆