0
我花了最近2天在cakephp中尋找分頁問題的解決方案。cakephp:擴展模型類以覆蓋分頁方法
總之,我需要不同的分頁方法爲同一模型。索引操作將使用默認方法,視圖操作將使用具有自定義查詢的方法。
基於OOP的繼承概念,我認爲解決方案是擴展我的模型並覆蓋擴展模型上的分頁方法。所以,我可以從控制器調用索引操作的默認分頁方法和視圖操作的自定義方法。
問題是:如何擴展我的模型類?將工作這個ideia?
這是我的模型:
<?php
class Communication extends AppModel {
var $name = 'Communication';
var $displayField = 'title';
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $hasMany = array(
'Interaction' => array(
'className' => 'Interaction',
'foreignKey' => 'communication_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Star' => array(
'className' => 'Star',
'foreignKey' => 'communication_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
var $hasAndBelongsToMany = array(
'Document' => array(
'className' => 'Document',
'joinTable' => 'communications_documents',
'foreignKey' => 'communication_id',
'associationForeignKey' => 'document_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'Event' => array(
'className' => 'Event',
'joinTable' => 'communications_events',
'foreignKey' => 'communication_id',
'associationForeignKey' => 'event_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'Meeting' => array(
'className' => 'Meeting',
'joinTable' => 'communications_meetings',
'foreignKey' => 'communication_id',
'associationForeignKey' => 'meeting_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => 'date_create DESC',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'User' => array(
'className' => 'User',
'joinTable' => 'communications_users',
'foreignKey' => 'communication_id',
'associationForeignKey' => 'user_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => 'name',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
function getComInfos($id) {
$this->unbindModel(array('hasAndBelongsToMany'=>array('Document','Event','Meeting')));
$this->unbindModel(array('hasMany'=>array('Star','Interaction')));
$communication_infos = $this->read(null, $id);
//debug($communication_infos);
return $communication_infos;
}
function getComLinks($id) {
$communication_links = $this->query("
SELECT 'documents' AS 'type', 'D' AS 'marker', d.id AS id, d.date_create AS date, du.user_id AS info_aux, d.title AS title, d.desc AS content
FROM communications_documents AS cd
JOIN documents AS d ON d.id = cd.document_id
JOIN documents_users AS du ON du.document_id = d.id
WHERE cd.communication_id = ".$id."
UNION
SELECT 'events' AS 'type', 'E' AS 'marker', e.id AS id, e.date_create AS date, e.institution AS info_aux, e.title AS title, e.desc AS content
FROM communications_events AS ce
JOIN events AS e ON e.id = ce.event_id
WHERE ce.communication_id =".$id."
UNION
SELECT 'meetings' AS 'type', 'R' AS 'marker', m.id AS id, m.date_create AS date, m.site AS info_aux, m.title AS title, m.desc AS content
FROM communications_meetings AS cm
JOIN meetings AS m ON m.id = cm.meeting_id
WHERE cm.communication_id =".$id."
UNION
SELECT 'interactions' AS 'type', 'C' AS 'marker', i.id AS id, i.date_create AS date, i.user_id AS info_aux, i.title AS title, i.content AS content
FROM interactions AS i
WHERE i.communication_id =".$id."
ORDER BY date DESC
");
//debug($communication_links);
return $communication_links;
}
}
這是我的兩個自定義分頁方法,我需要使用剛剛視圖操作:
function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {
$recursive = -1;
return $this->query("
SELECT 'documents' AS 'type', 'D' AS 'marker', d.id AS id, d.date_create AS date, du.user_id AS info_aux, d.title AS title, d.desc AS content
FROM communications_documents AS cd
JOIN documents AS d ON d.id = cd.document_id
JOIN documents_users AS du ON du.document_id = d.id
WHERE cd.communication_id = 137
UNION
SELECT 'events' AS 'type', 'E' AS 'marker', e.id AS id, e.date_create AS date, e.institution AS info_aux, e.title AS title, e.desc AS content
FROM communications_events AS ce
JOIN events AS e ON e.id = ce.event_id
WHERE ce.communication_id =137
UNION
SELECT 'meetings' AS 'type', 'R' AS 'marker', m.id AS id, m.date_create AS date, m.site AS info_aux, m.title AS title, m.desc AS content
FROM communications_meetings AS cm
JOIN meetings AS m ON m.id = cm.meeting_id
WHERE cm.communication_id = 137
UNION
SELECT 'interactions' AS 'type', 'C' AS 'marker', i.id AS id, i.date_create AS date, i.user_id AS info_aux, i.title AS title, i.content AS content
FROM interactions AS i
WHERE i.communication_id = 137
ORDER BY date DESC
LIMIT ".(($page-1)*$limit).", ".$limit);
}
function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$sql = "
SELECT 'documents' AS 'type', 'D' AS 'marker', d.id AS id, d.date_create AS date, du.user_id AS info_aux, d.title AS title, d.desc AS content
FROM communications_documents AS cd
JOIN documents AS d ON d.id = cd.document_id
JOIN documents_users AS du ON du.document_id = d.id
WHERE cd.communication_id = 137
UNION
SELECT 'events' AS 'type', 'E' AS 'marker', e.id AS id, e.date_create AS date, e.institution AS info_aux, e.title AS title, e.desc AS content
FROM communications_events AS ce
JOIN events AS e ON e.id = ce.event_id
WHERE ce.communication_id = 137
UNION
SELECT 'meetings' AS 'type', 'R' AS 'marker', m.id AS id, m.date_create AS date, m.site AS info_aux, m.title AS title, m.desc AS content
FROM communications_meetings AS cm
JOIN meetings AS m ON m.id = cm.meeting_id
WHERE cm.communication_id = 137
UNION
SELECT 'interactions' AS 'type', 'C' AS 'marker', i.id AS id, i.date_create AS date, i.user_id AS info_aux, i.title AS title, i.content AS content
FROM interactions AS i
WHERE i.communication_id = 137
ORDER BY date DESC
";
$this->recursive = $recursive;
$results = $this->query($sql);
return count($results);
}`
Hi @baseer, 你是對的,但如果我需要使用相同的模型呢?對於索引操作,我使用默認分頁方法,使用默認查詢。對於視圖操作,我需要一個自定義分頁方法和一個自定義查詢,這使得在某些表上選擇一個UNION。 –
您將需要重寫您的paginate()和paginateCount()方法。這裏是一個體面的例子:[Custom Query Pagination](http://book.cakephp.org/view/1237/Custom-Query-Pagination)。我將把 '$ results = $ this-> query($ sql);' 其中SQL片段'$ sql'將考慮'$ limit'和$ $ page傳入的變量(以及所有其他變量)。 要解決同一模型中多個分頁類型的問題,可以使用'$ extra'變量來區分它們。 – Baseer
酷@baseer。但是這個額外的工作怎麼樣?我從來沒有見過這種用途。 –