久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Doctrine 2,實體內(nèi)部查詢

Doctrine 2, query inside entities(Doctrine 2,實體內(nèi)部查詢)
本文介紹了Doctrine 2,實體內(nèi)部查詢的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

如何在實體中執(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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(shù))
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產(chǎn)品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 日韩精品一区二区三区视频播放 | 欧美精品一区二区三区在线 | 国产女人与拘做视频免费 | 中文字幕在线看人 | h视频在线免费看 | 天天干天天干 | 日韩电影一区二区三区 | 国产精品久久久久一区二区三区 | 亚洲高清在线观看 | 国产精品久久久久久久久久久久冷 | 欧美一区二区三区在线免费观看 | 亚洲另类视频 | 九九久久精品视频 | 欧美中文字幕一区 | 久久久美女 | 日本黄色短片 | 日韩精品一区二 | 欧美二区在线 | www.se91| 亚洲欧美bt| 日韩中文字幕在线 | 久久久一区二区三区 | 天堂久久久久久久 | 91九色porny首页最多播放 | a级性视频 | 成年人在线电影 | 日韩视频精品在线 | 亚洲一区二区三区在线视频 | 国产精品一区二区三级 | 四虎成人免费电影 | 性色的免费视频 | 亚洲欧美一区二区三区在线 | 久久久久久99 | 亚洲精品视频一区二区三区 | 亚洲一区二区三区四区五区中文 | 一二三区av | 免费精品久久久久久中文字幕 | 亚洲国产成人av好男人在线观看 | 欧美三级视频在线观看 | 91精品国产综合久久精品 | 一区在线播放 |