1

I have tree model with this structures and tables:

Rate:
id, model_name, object_id
1 , SocialPost, 12
public $belongsTo => array(
                'SocialPost' => array(
                    'className' => 'Social.SocialPost',
                    'foreignKey' => 'object_id',
                    'conditions' => array(
                        'Rate.object_id = SocialPost.id',
                    ),
                )
            )

SocialPost: id, file_id

public $hasOne = array(
    'File' => array(
        'className' => 'File',
        'foreignKey' => false,
        'conditions' => array(
            'File.id = SocialPost.file_id',
        ),
    ),
);

File: id, title


all tree models actsAs containable

this code work nice in SocialPostsController:

$posts = $this->SocialPost->find('all', array(
        'limit' => 5,
        'contain' => array(
            'File'
        )

    ));

output: http://pastie.org/private/9ixxufncwlr3tofgp8ozw

but this code in RatesController return same file for all SocialPost:

$mostRated = $this->Rate->find('all', array(
        'limit' => $count,
        'contain' => array(
            'SocialPost' => array(
                'File'
            )
        )

    ));

output: http://pastie.org/private/lbqryo1gxgvxjb5omfwrw

what is wrong here?

Community
  • 1
  • 1
realman
  • 95
  • 5

1 Answers1

1

I think your associations should all be belongsTo:

class Rate extends AppModel {
  public $belongsTo = array(
    'SocialPost' => array(
      'className' => 'Social.SocialPost',
      'foreignKey' => 'object_id',
    )
  );
}

class SocialPost extends AppModel {
  public $belongsTo = array(
    'File'
  );
}

And then your find command can look like:

$mostRated = $this->Rate->find('all', array(
  'limit' => $count,
  'contain' => array(
    'SocialPost.File'
  )
));

Also, I would double-check that your 'className' => 'Social.SocialPost' is correct. This means that the SocialPost model exists in a plugin called Social.

ifunk
  • 627
  • 4
  • 10
  • Social is plugin and SocialPost is its model. I cannot use belongsTo for File in SocialPost.because of every post has one file and in File model I have belongsTo for SocialPost.every file belongs to many SocialPost. – realman Jun 30 '12 at 15:05