1

我有这个结构和表格的树模型:

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',
                    ),
                )
            )

社会邮报:id,file_id

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

文件:id,标题


所有树模型都作为可包含的

此代码在 SocialPostsController 中运行良好:

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

    ));

输出: http: //pastie.org/private/9ixxufncwlr3tofgp8ozw

但是 RatesController 中的这段代码为所有 SocialPost 返回相同的文件:

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

    ));

输出: http: //pastie.org/private/lbqryo1gxgvxjb5omfwrw

这里有什么问题?

4

1 回答 1

1

我认为您的关联都应该属于:

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

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

然后你的 find 命令看起来像:

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

另外,我会仔细检查您'className' => 'Social.SocialPost'是否正确。这意味着该SocialPost模型存在于一个名为Social.

于 2012-06-30T13:32:24.323 回答