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


Check if a model is associated with another in cakephp?

我使用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

如果没有这样的函数,我该如何编写自己的函数?我讲不通逻辑。请提出建议。

您可以使用函数getAssociated

$this->yourModel->getAssociated();

并返回与相关的型号

http://book.cakephp.org/2.0/en/models/additional-methods-and-properties.html#model-getassociated字符串类型为null