findByExample in Doctrine(教义中的 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 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:教义中的 findByExample
基础教程推荐
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- php中的foreach复选框POST 2021-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- php中的PDF导出 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
