如何在实体中执行查询?
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模板网!
以编程方式将可下载文件添加到 Woocommerce 产品Add programmatically a downloadable file to Woocommerce products(以编程方式将可下载文件添加到 Woocommerce 产品)
获取今天 Woocommerce 中每种产品的总订单数Get today#39;s total orders count for each product in Woocommerce(获取今天 Woocommerce 中每种产品的总订单数)
在 WooCommerce 和电话字段验证问题中添加自定义注Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和电话字段验证问题中添加自定义注册字段
在 Woocommerce 简单产品中添加一个将更改价格的选Add a select field that will change price in Woocommerce simple products(在 Woocommerce 简单产品中添加一个将更改价格的选择字段)
在 WooCommerce 3 中将自定义列添加到管理产品列表Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中将自定义列添加到管理产品列表)
自定义结帐“下订单"按钮输出htmlCustomizing checkout quot;Place Orderquot; button output html(自定义结帐“下订单按钮输出html)