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

Doctrine 2,實體內部查詢

Doctrine 2, query inside entities(Doctrine 2,實體內部查詢)
本文介紹了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模板網!

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

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
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 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 97超碰免费 | 国产福利视频在线 | 亚洲精品一二三 | 国产黄色片视频 | 日本久久精品 | 成 人 黄 色 片 在线播放 | 欧美精品在线播放 | 精品国产一区二区三区四区 | www.久草 | 精品国产va久久久久久久 | 日韩综合久久 | 久久久久久91 | 四虎wz| 国产一区二区免费看 | 婷婷色在线 | 国产91精品看黄网站在线观看 | www国产在线观看 | 三级视频网 | www.久久 | 国产又粗又猛又黄又爽无遮挡 | 日韩精品一 | 国产美女自拍视频 | 谁有毛片网址 | 日韩在线看片 | 日韩精品久久久久 | 日韩精品视频在线 | 在线不卡一区二区 | 神马影院午夜伦理片 | 欧美激情一二三区 | 日韩影音 | www.九色| 毛片免费播放 | 色片在线 | 欧美日韩视频在线 | 午夜国产一区 | 亚洲国产精品久久 | 蜜臀久久99精品久久久久久宅男 | 中文字幕偷拍 | 免费看a级片 | 国产一级免费视频 | 亚洲综合在线视频 |