Doctrine 2,实体内部查询

Doctrine 2, query inside entities(Doctrine 2,实体内部查询)
本文介绍了Doctrine 2,实体内部查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何在实体中执行查询?

How do I perform queries in an entity?

namespace EntitiesMembers;

/**
 * @Entity(repositoryClass="EntitiesMemberMembersRepository")
 * @Table(name="Members")
 * @HasLifecycleCallbacks
 */
class Members extends EntitiesAbstractEntity
{
    /**
     * @Id @Column(name="id", type="bigint",length=15)
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /** 
     * @Column(name="userid", type="bigint", length=26, nullable=true) 
     */
    protected $userid;

    /** 
     * @Column(name="fname", type="string", length=255,nullable=true) 
     */
    protected $fname;

    /**
     *  @OneToMany(targetEntity="EntitiesUsersWall", mappedBy="entry", cascade={"persist"}) 
     */
    protected $commententries;

    public function __construct()
    {
        $this->commententries = new DoctrineCommonCollectionsArrayCollection();
    }
}

例如,我想在这个实体中有一个函数,称为:filter()我希望能够过滤 commententries 集合.它应该返回具有特定条件的集合,例如 id=1.基本上它应该过滤从连接查询收到的数据.

Example I would like to have a function inside this entity called: filter() and I want to be able to filter the commententries collection. It should return a collection with a certain condition such id=1. Basically it should be filtering the data received from the join query.

就像这样:

$this->commententries->findBy(array('id' => 1));

但显然这行不通.

推荐答案

一般来说,你不应该这样做.

Generally speaking, you shouldn't do this.

根据经验,实体不应该知道实体管理器(直接或通过某些中间对象).

Entities, as a rule of thumb, should not know about the entitymanager (directly, or via some intermediary object).

这样做的原因主要是可测试性,但根据我的经验,它有助于以其他方式组织事情.

The reason for this is mostly testability, but in my experience, it helps keeps things organized in other ways.

我会通过设计一个服务类来为您处理查找.您的控制器(或其他任何东西)会像这样驱动它:

I'd approach it by designing a service class that handles the lookups for you. Your controller (or whatever) would drive it like this:

<?php
// create a new service, injecting the entitymanager.  if you later wanted 
// to start caching some things, you might inject a cache driver as well.
$member = $em->find('Member',$member_id); //get a member, some how.
$svc = new MemberService($em);

$favoriteCommentaries = $svc->getFavoriteCommentaries($member);

正如我在评论中暗示的那样,如果您稍后决定要添加缓存(例如,通过 memcached)以避免频繁查找,您可以在此服务类附近或中的某个位置执行此操作.这使您的实体保持良好和简单,并且易于测试.由于您在构建时将实体管理器注入到服务中,因此您可以根据需要进行模拟.

As I hint in the comment, if you decide later that you want to add caching (via memcached, for instance) to avoid frequent lookups, you'd do that somewhere near or in this service class. This keeps your entities nice and simple, and easily testable. Since you inject your entitymanager into the service at construction-time, you can mock that as needed.

getFavoriteCommentaries() 可以使用各种实现.一个简单的方法是将它代理到 Member::getFavoriteCommentaries(),它实际上会加载所有内容,然后过滤掉最喜欢的".这可能不会特别好地扩展,因此您可以通过使用 EM 仅获取您需要的数据来改进它.

getFavoriteCommentaries() could use various implementations. A trivial one would be to proxy it to Member::getFavoriteCommentaries(), which would actually load everything, and then filter out the "favorite" ones. That probably won't scale particularly well, so you could improve it by using the EM to fetch just the data you need.

这篇关于Doctrine 2,实体内部查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)