1

I am using below code but Cakephp get out error : "Model "Comment" is not associated with model "User""

$this->Paginator->settings = array(
            'contain' => array_merge(
                    array(
                        'Comment' => array(
                            'limit' => 1,
                            'User' => array(
                                'fields' => array('username','id'),
                            ),
                        )
                    ),
            ),
            'recursive' => 1,
            'conditions' => $conditions,
            'limit' => 10,                    
    );

    $posts = $this->Paginator->paginate('Post');

In User model:

public $hasMany = array(
     'Comment' => array(
        'className' => 'Comment',
        'foreignKey' => 'author',
        'dependent' => false
    ),
);

And in Comment model :

    public $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'author',
    )
);
  • Did you add `public $actsAs = array('Containable'); at the beginning of your models? Or did you experiment with that? – Jelmer Sep 14 '13 at 09:35
  • Yes I was added that. Cause of my problem is associating Comment to User model using Containable: 'User' => array( 'fields' => array('username','id') ) But Cakephp get out error ! – user2665477 Sep 21 '13 at 12:45
  • can you var_dump the Paginator::settings right after the declaration of the variable? So that we know what $conditions contains and what actually is the value of this array? – Jelmer Sep 23 '13 at 13:41

1 Answers1

0

Firstly you shouldnt use

'recursive' => 1
with Containable behavior. This behavior is to get related models as you want, so in you AppModel class add
public $recursive = 0
. Next in you controller try:
$this->paginate['User'] = array(
            'conditions' => $conditions,
            'limit' => 10, 
            'contain' => array(
                        'Comment' => array(
                            'limit' => 1,
                            'User' => array(
                                'fields' => array('username','id'),
                            ),
                        )
            ),
);
Heroes84
  • 934
  • 1
  • 9
  • 18