【发布时间】:2016-07-12 10:04:23
【问题描述】:
我对 CakePHP 2.x 文档 Model Association 感到困惑,所以在这里需要一点帮助来连接并在结果中查找包含。
Deal Table > id | title
DealAttribute Table (has options) > id | title
DealAttributeOption Table (belongs to DealAttribute) > id | title | deal_attribute_id
DealsDealAttributes Table > id | deal_id | deal_attribute_id | option_id
需要类似的结果
Deal [
id, title
DealsDealAttributes [
id, deal_id, deal_attribute_id, option_id
DealAttribute [
title
]
DealAttributeOption [
title
]
]
]
我尝试使用$belongsTo 以及DealsDealAttributes 中的$hasAndBelongsToMany 以及所有三个Deal, DealAttribute, DealAttributeOption,但没有得到包含。
现在我想如果我找到任何交易,那么所有相关的模型都将包含在内。如何建立模特协会?
【问题讨论】:
-
您是否定义了 $hasMany 和 $belongsTo 与各自模型的关系?还是您对此感到困惑?如果你遵循 cakephp 制作表格的约定,那么你也可以添加与蛋糕烘焙的模型关系..
-
查看您现有的关联代码以了解您迄今为止所做的尝试会很有帮助。基本上,包含外键的表的模型具有
belongsTo关系,而与包含外键的另一个表关联的模型将是hasOne或hasMany关系。 -
@drmonkeyninja 我尝试了很多方法,但没有人工作,所以很困惑你能告诉我正确的方向吗?
-
@Anupal 从您当前的问题来看,您需要什么有点令人困惑。您能否包含架构的图像?
-
@drmonkeyninja 使用架构图像更新问题。
标签: php cakephp model associations