問題描述
如何在實體中執(zhí)行查詢?
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();
}
}
例如,我想在這個實體中有一個函數(shù),稱為:filter()
我希望能夠過濾 commententries
集合.它應(yīng)該返回具有特定條件的集合,例如 id=1
.基本上它應(yīng)該過濾從連接查詢收到的數(shù)據(jù).
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));
但顯然這行不通.
推薦答案
一般來說,你不應(yīng)該這樣做.
Generally speaking, you shouldn't do this.
根據(jù)經(jīng)驗,實體不應(yīng)該知道實體管理器(直接或通過某些中間對象).
Entities, as a rule of thumb, should not know about the entitymanager (directly, or via some intermediary object).
這樣做的原因主要是可測試性,但根據(jù)我的經(jīng)驗,它有助于以其他方式組織事情.
The reason for this is mostly testability, but in my experience, it helps keeps things organized in other ways.
我會通過設(shè)計一個服務(wù)類來為您處理查找.您的控制器(或其他任何東西)會像這樣驅(qū)動它:
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)以避免頻繁查找,您可以在此服務(wù)類附近或中的某個位置執(zhí)行此操作.這使您的實體保持良好和簡單,并且易于測試.由于您在構(gòu)建時將實體管理器注入到服務(wù)中,因此您可以根據(jù)需要進行模擬.
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() 可以使用各種實現(xiàn).一個簡單的方法是將它代理到 Member::getFavoriteCommentaries(),它實際上會加載所有內(nèi)容,然后過濾掉最喜歡的".這可能不會特別好地擴展,因此您可以通過使用 EM 僅獲取您需要的數(shù)據(jù)來改進它.
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.
這篇關(guān)于Doctrine 2,實體內(nèi)部查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!