CakePHP 2.2.2插件分页
我目前正在尝试使用containable with$this->paginate,但是我无法在结果集中获得额外的表。我希望结果集中有MediaItem和MediaCollectionItem 下面是我的索引操作的控制器代码CakePHP 2.2.2插件分页,php,cakephp,pagination,cakephp-2.2,containable,Php,Cakephp,Pagination,Cakephp 2.2,Containable,我目前正在尝试使用containable with$this->paginate,但是我无法在结果集中获得额外的表。我希望结果集中有MediaItem和MediaCollectionItem 下面是我的索引操作的控制器代码 App::uses('MediaAppController', 'Media.Controller'); class ItemsController extends MediaAppController { var $uses = array("Me
App::uses('MediaAppController', 'Media.Controller');
class ItemsController extends MediaAppController {
var $uses = array("Media.MediaItem", "Media.MediaCollection");
var $components = array("Media.MediaRender","Media.TimThumb");
var $helpers = array("Media.MediaRender");
var $mediaItemDetails = null;
var $paginate = array('limit' => 15, 'contain' => array('Media.MediaCollectionItem'));
function beforeFilter() {
parent::beforeFilter();
$this -> Auth -> allow("resizeImage");
}
function admin_index() {
$this -> request -> data = $this->paginate('MediaItem');
debug($this -> request -> data);
// Get images group
$filters = $this -> MediaRender -> getCollectionFilterTypes(array("images"));
$renderImageTypes = array_keys($filters["images"]["extensions"]);
$this -> set(compact("renderImageTypes"));
}
下面是我的MediaItem模型,它具有所有关系
App::uses('MediaAppModel', 'Media.Model');
class MediaItem extends MediaAppModel {
/**
* Display field
*
* @var string
*/
var $displayField = 'name';
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* hasMany associations
*
* @var array
*/
var $hasMany = array(
'MediaCollectionItem' => array(
'className' => 'Media.MediaCollectionItem',
'foreignKey' => 'media_item_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'MediaCollection' => array(
'className' => 'Media.MediaCollection',
'foreignKey' => 'media_item_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'media_item_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
App::uses('MediaAppModel', 'Media.Model');
class MediaCollectionItem extends MediaAppModel {
/**
* Display field
*
* @var string
*/
public $displayField = 'title';
public $order = array("MediaCollectionItem.sort_order ASC");
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'MediaCollection' => array(
'className' => 'Media.MediaCollection',
'foreignKey' => 'media_collection_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'MediaItem' => array(
'className' => 'Media.MediaItem',
'foreignKey' => 'media_item_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
下面是我的MediaCollectionItem模型,它具有所有关系
App::uses('MediaAppModel', 'Media.Model');
class MediaItem extends MediaAppModel {
/**
* Display field
*
* @var string
*/
var $displayField = 'name';
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* hasMany associations
*
* @var array
*/
var $hasMany = array(
'MediaCollectionItem' => array(
'className' => 'Media.MediaCollectionItem',
'foreignKey' => 'media_item_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'MediaCollection' => array(
'className' => 'Media.MediaCollection',
'foreignKey' => 'media_item_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'media_item_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
App::uses('MediaAppModel', 'Media.Model');
class MediaCollectionItem extends MediaAppModel {
/**
* Display field
*
* @var string
*/
public $displayField = 'title';
public $order = array("MediaCollectionItem.sort_order ASC");
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'MediaCollection' => array(
'className' => 'Media.MediaCollection',
'foreignKey' => 'media_collection_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'MediaItem' => array(
'className' => 'Media.MediaItem',
'foreignKey' => 'media_item_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
插件应用模型
class MediaAppModel extends AppModel {
public $actsAs = array("Containable");
}
当前结果的示例
array(
(int) 0 => array(
'MediaItem' => array(
'id' => '1098',
'name' => 'custom',
'extension' => 'css',
'file_size' => '0',
'mime_type' => 'application/x-empty',
'filename' => '2538984824fdb12ed8c8151.31598776.custom',
'created' => '2012-06-15 11:48:13',
'modified' => '2012-06-15 11:48:13',
'remote_url' => null
)
),
(int) 1 => array(
'MediaItem' => array(
'id' => '1099',
'name' => 'style',
'extension' => 'css',
'file_size' => '39002',
'mime_type' => 'text/plain; charset=us-ascii',
'filename' => '867449124fdb12fa1d3d11.81854984.style',
'created' => '2012-06-15 11:48:26',
'modified' => '2012-06-15 11:49:08',
'remote_url' => null
)
),
控制器内MediaItem模型的调试
object(AppModel) {
useDbConfig => 'default'
useTable => 'media_items'
id => false
data => array()
schemaName => 'cbsroxy_cms'
table => 'media_items'
primaryKey => 'id'
validate => array()
validationErrors => array()
validationDomain => null
name => 'MediaItem'
alias => 'MediaItem'
tableToModel => array(
'media_items' => 'MediaItem'
)
cacheQueries => false
belongsTo => array()
hasOne => array()
hasMany => array()
hasAndBelongsToMany => array()
actsAs => null
Behaviors => object(BehaviorCollection) {
modelName => 'MediaItem'
defaultPriority => (int) 10
}
whitelist => array()
cacheSources => true
findQueryType => null
recursive => (int) 1
order => null
virtualFields => array()
__backAssociation => array()
__backInnerAssociation => array()
__backOriginalAssociation => array()
__backContainableAssociation => array()
findMethods => array(
'all' => true,
'first' => true,
'count' => true,
'neighbors' => true,
'list' => true,
'threaded' => true
)
tablePrefix => ''
我现在已经设法让它工作了。我必须将插件模型的前缀设置为media,然后将类名设置为media.Item,并调用$this->Item->find以使可包含的行为正常工作。我目前只在我的结果中获取MediaItem模型如果你进行分页怎么办;而不是将“MediaItem”分页?遗憾的是,它返回了相同的结果。我现在已经设法让它工作了。我必须将插件模型的前缀设置为media,然后将类名设置为media.Item,并调用$this->Item->find来实现可包含的行为。太棒了!你应该给你的问题添加答案,这样每个人都能看到。