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 PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
