1

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 => ''
Chuck Burgess
  • 11,600
  • 5
  • 41
  • 74
adamias
  • 77
  • 11
  • I am currently only getting the MediaItem model in my results – adamias Sep 08 '12 at 18:20
  • what if you do `paginate();` instead of `paginate('MediaItem');` ? – Choma Sep 08 '12 at 18:27
  • Sadly it returns the same results – adamias Sep 08 '12 at 18:29
  • Are you attaching Containable behavior on model or controller?? I can't see it in your code. – Choma Sep 08 '12 at 18:48
  • It is on the AppModel for the plugin. – adamias Sep 08 '12 at 19:09
  • if I debug $this->MediaItem it would appear none of the association's are available. – adamias Sep 08 '12 at 19:18
  • If the behavior is attached, it sounds like it is a model association problem. Have you tried retrieving your data with `find()`? what happen if you set `'recursive' => 2`? – Choma Sep 08 '12 at 19:18
  • Same thing happens. I have updated the code above. If you look at the last code block you will see it doesn't recognise anything specified in the model. – adamias Sep 08 '12 at 19:28
  • Your sintaxis in model associations is incorrect, look at [this](http://book.cakephp.org/2.0/en/plugins.html#plugin-models) – Choma Sep 08 '12 at 19:37
  • Strange as the models were generated using the console app – adamias Sep 08 '12 at 19:54
  • Just looked at another plugin for CakePHP 2.2 and they have the same setup for their models. What is exactly wrong with mine? – adamias Sep 08 '12 at 19:59
  • 2
    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. – adamias Sep 09 '12 at 12:45
  • 6
    Great! you should add the solution to your question so everyone can see it. – Choma Sep 09 '12 at 13:32
  • -1 until you add your comment-answer as an answer and accept it =). – AD7six Mar 16 '13 at 17:01

1 Answers1

0

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.

adamias
  • 77
  • 11