检查模型是否与cakephp中的另一个模型相关联?

时间:2013-08-02 08:52:14

标签: php cakephp-2.0

我正在使用CakePHP一段时间并遇到一个问题,我需要检查模型是否与其他模型相关联。那有内置功能吗?

示例

BLOG
 -- hasMany COMMENTS
 -- hasOne Profile

COMMENTS
 -- belongsTo BLOG

我正在寻找一些功能:

hasAssociation('blog', 'comments'); //should return true
hasAssociation('blog', 'whatever'); //should return false
hasAssociation('profile', 'blog'); //should return false
hasAssociation('blog', 'profile'); //should return true

如果没有这样的功能怎么写我自己的?我不合逻辑。请建议。

1 个答案:

答案 0 :(得分:3)

你可以使用函数getAssociated

$this->yourModel->getAssociated();

并返回相关模型

http://book.cakephp.org/2.0/en/models/additional-methods-and-properties.html#model-getassociated-string-type-null

相关问题