Doctrine 2,实体内部查询

时间:2023-04-21
本文介绍了Doctrine 2,实体内部查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

如何在实体中执行查询?

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,实体内部查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何使用 PHP 在白名单中允许 HTML 下一篇:为什么使用 filter_input() 更好?

相关文章

最新文章