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

CakePHP - HABTM 分頁查詢的問題

CakePHP - problem with HABTM paginate query(CakePHP - HABTM 分頁查詢的問題)
本文介紹了CakePHP - HABTM 分頁查詢的問題的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

表格

restaurants
cuisines
cuisines_restaurants

餐廳和美食模型都相互設置為 HABTM.

Both restaurant and cuisine model are set up to HABTM each other.

我正在嘗試獲取分頁的餐廳列表,其中 Cuisine.name = 'italian'(示例),但不斷收到此錯誤:

I'm trying to get a paginated list of restaurants where Cuisine.name = 'italian' (example), but keep getting this error:

1054: Unknown column 'Cuisine.name' in 'where clause'

正在構建的實際查詢:

SELECT `Restaurant`.`id`, `Restaurant`.`type` ..... 
`Restaurant`.`modified`, `Restaurant`.`user_id`, `User`.`display_name`,
`User`.`username`, `User`.`id`, `City`.`id`,`City`.`lat`  ..... 
FROM `restaurants` AS `Restaurant` LEFT JOIN `users` AS `User` ON 
(`Restaurant`.`user_id` = `User`.`id`) LEFT JOIN `cities` AS `City` ON 
(`Restaurant`.`city_id` = `City`.`id`) WHERE `Cuisine`.`name` = 'italian' 
LIMIT 10

....."部分只是我刪除的附加字段,以縮短向您展示的查詢.

The "....." parts are just additional fields I removed to shorten the query to show you.

我不是 CakePHP 專家,所以希望有一些明顯的錯誤.我像這樣調用分頁:

I'm no CakePHP pro, so hopefully there's some glaring error. I'm calling the paginate like this:

$this->paginate = array(
    'conditions' => $opts,
    'limit' => 10,
);
$data = $this->paginate('Restaurant');
$this->set('data', $data);

$opts 是一組選項,其中一個是 'Cuisine.name' => 'italian'

$opts is an array of options, one of which is 'Cuisine.name' => 'italian'

我也嘗試設置 $this->Restaurant->recursive = 2;但這似乎沒有任何作用(我認為我不應該這樣做?)

I also tried setting $this->Restaurant->recursive = 2; but that didn't seem to do anything (and I assume I shouldn't have to do that?)

非常感謝任何幫助或指導.

Any help or direction is greatly appreciated.

編輯

models/cuisine.php
    var $hasAndBelongsToMany = array('Restaurant');

models/restaurant.php
    var $hasAndBelongsToMany = array(
    'Cuisine' => array(
        'order' => 'Cuisine.name ASC'
    ),
    'Feature' => array(
        'order' => 'Feature.name ASC'
    ),
    'Event' => array(
        'order' => 'Event.start_date ASC'
    )
);

推薦答案

這失敗了,因為 Cake 實際上使用了 2 個不同的查詢來生成結果集.正如您所注意到的,第一個查詢甚至不包含對 Cuisine 的引用.

This fails because Cake is actually using 2 different queries to generate your result set. As you've noticed, the first query doesn't even contain a reference to Cuisine.

正如@vindia 在此處所解釋的那樣,使用 Containable 行為將通常解決這個問題,但是它不適用于 Paginate.

As @vindia explained here, using the Containable behavior will usually fix this problem, but it doesn't work with Paginate.

基本上,您需要一種方法來強制 Cake 在第一次查詢期間查看 Cuisine.這不是框架通常的工作方式,因此不幸的是,它確實需要 手動構建連接.paginate 采用與 Model->find('all') 相同的選項.在這里,我們需要使用 joins 選項.

Basically, you need a way to force Cake to look at Cuisine during the first query. This is not the way the framework usually does things, so it does, unfortunately, require constructing the join manually . paginate takes the same options as Model->find('all'). Here, we need to use the joins option.

var $joins = array(
    array(
        'table' => '(SELECT cuisines.id, cuisines.name, cuisines_restaurants.restaurant_id
                 FROM cuisines_restaurants 
                 JOIN cuisines ON cuisines_restaurants.cuisines_id = cuisines.id)',
        'alias' => 'Cuisine',
        'conditions' => array(
            'Cuisine.restaurant_id = Restaurant.id',
            'Cuisine.name = "italian"'
        )
    )
);

$this->paginate = array(
    'conditions' => $opts,
    'limit' => 10,
    'joins' => $joins
);

這個解決方案比其他解決方案笨拙得多,但具有工作優勢.

This solution is a lot clunkier than the others, but has the advantage of working.

這篇關于CakePHP - HABTM 分頁查詢的問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
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)(致命錯誤:允許的內存大小為 134217728 字節已用盡(嘗試分配 87 字節))
主站蜘蛛池模板: 中文字幕亚洲精品 | 成人影视网 | av香蕉 | 亚洲精品视频一区二区三区 | 999热在线视频 | 99精品久久 | 白浆在线 | 另类视频在线 | 精品久久久久久红码专区 | 天天操夜夜骑 | 久热久草| 久久久久久亚洲精品 | 国产精品美女久久久久久免费 | 亚洲综合色 | 日韩欧美国产一区二区三区 | 我爱操 | 日韩福利 | 久久久久久国产一区二区三区 | 精品视频一区二区三区四区 | 色婷婷av久久久久久久 | 国产区精品在线观看 | 免费人成激情视频在线观看冫 | 在线观看亚洲欧美 | 中国美女撒尿txxxxx视频 | 欧美色a v| 成人性视频免费网站 | 乱码av午夜噜噜噜噜动漫 | 国产精品成人国产乱一区 | 91久久国产综合久久 | 自拍 亚洲 欧美 老师 丝袜 | 国外成人在线视频网站 | 日韩国产一区二区三区 | 欧美日韩成人在线 | 天天干天天操天天看 | 日韩一二区 | 成人在线一区二区 | 免费在线观看黄网站 | 精品久久香蕉国产线看观看亚洲 | 蜜桃久久 | 久久久久亚洲精品国产 | 国产精品小视频在线观看 |