编程语言
首页 > 编程语言> > 检查一个模型是否与cakephp中的另一个模型相关联?

检查一个模型是否与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-string-type-null

标签:cakephp-2-0,php
来源: https://codeday.me/bug/20191123/2063838.html