I am currently trying to use containable with $this->paginate() however I am unable to get the extra table in my result set. I am expecting to have MediaItem and MediaCollectionItem in the result set.
Below is the controller code I have for my index action
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"));
}
Below is my MediaItem Model which has all the relationships
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' => ''
)
);
}
Below is my MediaCollectionItem Model which has all the relationships
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' => ''
)
);
}
Plugin AppModel
class MediaAppModel extends AppModel {
public $actsAs = array("Containable");
}
Example of current results
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
)
),
Debug of MediaItem model inside the controller
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 => ''
I have now managed to get it working. I have had to set the prefix for the plugins models to "media_" and then have the class names as "Media.Item" and call "$this->Item->find()" to get the containable behaviour to work.