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

我可以在 cakephp3 的 Table 類上設(shè)置默認(rèn)順序嗎

Can I set the default order on the Table class on cakephp3(我可以在 cakephp3 的 Table 類上設(shè)置默認(rèn)順序嗎)
本文介紹了我可以在 cakephp3 的 Table 類上設(shè)置默認(rèn)順序嗎的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

在 CakePHP 2.x 中,模型中有一個(gè)屬性 $order.所以我使用這個(gè)屬性來(lái)對(duì)我的數(shù)據(jù)進(jìn)行全局排序.例如,假設(shè)我需要在用于添加行的 Country 模型中的視圖中顯示一個(gè)帶有國(guó)家/地區(qū)的選擇框:

In CakePHP 2.x there was a property $order in Models. So I used this property to order my data globally. So for example assuming that I need to show a select box with countries on a view in my Country model used to add the line:

$order = 'Country.country DESC';

然后當(dāng)我從任何控制器獲取國(guó)家/地區(qū)時(shí),數(shù)據(jù)按國(guó)家/地區(qū)名稱排序,而不是按 id 或任何其他字段排序.這對(duì)選擇框特別有用.在 CakePHP 3.x 上,我似乎在文檔中找不到任何類似的參考.

and then when I fetched the countries from any controller the data where ordered by the country name and not by the id or any other field. This was very helpful specially for the select boxes. On CakePHP 3.x I can't seem to find any similar reference at the documentation.

有什么辦法可以讓我在獲取數(shù)據(jù)時(shí)對(duì)數(shù)據(jù)進(jìn)行全局排序,而不是在每次查找中使用 order 選項(xiàng)?

Is there anything that I can do to have my data sorted globally when I fetch them and not use the order option in each find?

推薦答案

只需添加您心愛(ài)的屬性并使用 beforeFind() 回調(diào) Table 對(duì)象中的值從屬性添加到查詢中.

Just add your beloved property back and use the beforeFind() callback in the Table object to add the value from the property to the query.

或者只是創(chuàng)建自定義查找器:

public function findOrdered(Query $query, $options) {
    return $query->order([
        $this->alias() . '.name' => 'ASC'
    ]);
}

并使用它

$this->find('list')->find('ordered')->all();

或者創(chuàng)建一個(gè)有序列表 find 來(lái)返回整個(gè)有序列表.

Or create an ordered list find that returns the whole ordered list.

public function findOrderedList(Query $query, $options) {
    return $this->findList($query, $options)
    ->order([
        $this->alias() . '.name' => 'ASC'
    ]);
}

或者直接重載 findList() 方法并調(diào)用父級(jí).

Or overload the findList() method directly and call the parent.

或者如果您的 find() 通過(guò)關(guān)系被調(diào)用,您可以 使用 sort 選項(xiàng)設(shè)置關(guān)系的默認(rèn)順序.

Or if your find() gets called via a relationship, you can set the default order for the relationship by using the sort option.

$this->hasMany('AuditLogs', [
    'sort' => [
        'AuditLogs.timestamp' => 'desc',
    ],
]);

這篇關(guān)于我可以在 cakephp3 的 Table 類上設(shè)置默認(rèn)順序嗎的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Session is lost after an OAuth redirect(OAuth 重定向后會(huì)話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁(yè)排序)
CakePHP Shared core for multiple apps(CakePHP 多個(gè)應(yīng)用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯(cuò)誤:允許的內(nèi)存大小為 134217728 字節(jié)已用盡(嘗試分配 87 字節(jié)))
主站蜘蛛池模板: 中文成人在线 | 在线观看涩涩视频 | 国产毛片毛片 | 国产高清一区二区三区 | 欧美视频免费在线 | 8x国产精品视频一区二区 | 日韩一级电影免费观看 | 亚洲高清在线观看 | 国产精品成av人在线视午夜片 | a级黄色片在线观看 | 亚洲国产一区在线 | 成人在线精品视频 | 久久久久久av | aaaaaaa片毛片免费观看 | 精品国产一区二区三区观看不卡 | 精产国产伦理一二三区 | www国产成人免费观看视频,深夜成人网 | 91麻豆精品国产91久久久久久 | 久久国产区 | 欧美日韩在线免费观看 | 日韩久久综合网 | www.97zyz.com | 天天干天天操天天射 | 精品av| 亚洲在线电影 | a级免费黄色片 | 女人毛片a毛片久久人人 | 欧美乱操| 婷婷色婷婷 | 在线播放中文字幕 | 伊人一二三 | 亚洲成人在线视频播放 | 日韩欧美手机在线 | 久久国产精品视频 | 亚洲精品一二三区 | 性精品 | 久久久国产一区二区三区四区小说 | 狠狠色综合网站久久久久久久 | 一级久久久久久 | 欧美高清视频一区 | 乳色吐息在线观看 |