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

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

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

問題描述

限時送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 集合.它應該返回具有特定條件的集合,例如 id=1.基本上它應該過濾從連接查詢收到的數(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));

但顯然這行不通.

推薦答案

一般來說,你不應該這樣做.

Generally speaking, you shouldn't do this.

根據(jù)經(jī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.

我會通過設計一個服務類來為您處理查找.您的控制器(或其他任何東西)會像這樣驅(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)以避免頻繁查找,您可以在此服務類附近或中的某個位置執(zhí)行此操作.這使您的實體保持良好和簡單,并且易于測試.由于您在構建時將實體管理器注入到服務中,因此您可以根據(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.

這篇關于Doctrine 2,實體內(nèi)部查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(liá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(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 日韩亚洲一区二区 | 精品久久久久久亚洲精品 | 中文字幕 在线观看 | 亚洲欧美久久 | 青青草av网站 | 欧美日韩在线一区二区三区 | 久久不卡 | 天天草天天干 | 国产美女在线观看 | 亚洲国产精品久久久久秋霞不卡 | 成年人在线观看 | 久久国产精品一区二区三区 | 亚洲色图在线观看 | 精品美女在线观看 | 在线播放国产视频 | 欧美一区在线视频 | 欧美大片黄 | 凹凸日日摸日日碰夜夜 | 久久久久91 | 久在线观看 | 亚洲欧美一区二区三区在线 | 国产精品网址 | 亚洲国产成人久久综合一区,久久久国产99 | 一级大片免费 | 在线不卡av | 羞羞视频免费在线观看 | av黄色在线观看 | 国产精品视频观看 | 国产线视频精品免费观看视频 | 二区久久 | 精品国产乱码一区二区三区a | 亚洲影音先锋 | 久久久久国产精品一区二区 | 免费一区 | 国产精品欧美大片 | 国产高清在线 | 国产精品久久久久久久久久妇女 | 成人自拍视频 | 粉嫩一区二区三区四区公司1 | 一区二区视频在线 | 黄片毛片在线观看 |