CakePHP 模型设计 - 更好的方法?
我有一个模型“目标”,它有四种类型的子项(它们都有不同的字段):
campaign_roi 呼叫成功 电话技能 call_qluality
目标表包含一个字段“model”,它指定上述四个模型之一。
但一个“目标”只有4个孩子中的一个。他们永远不会有两种类型的孩子。还有比为每个孩子创建一个模型更好的方法吗?如果不是,我如何动态指定目标是哪种子类型?
例如,我想查看一个目标及其子目标 - 在本例中为 Campaign_roi。我如何设置它,我可以做到这一点
$this->Goal->find('first', array(
'conditions' => array(
'Goal.id' => $id
)
)
,它从目标模型及其子campaign_roi返回信息?
I have a model "Goal" that has four types of children (which all have different fields):
campaign_roi
call_success
phone_skill
call_qluality
The goal table contains a field "model" which specifies one of the four models above.
But one "Goal" only has one of the 4 children. They will never have two types of children. Is there a better way than creating a model for each child? And if not, how can I dynamically specify which child type any goal is?
For instance, I want to see a goal and it's child - in this case a campaign_roi. How can I set it up where I can do this
$this->Goal->find('first', array(
'conditions' => array(
'Goal.id' => $id
)
)
And it returns information from the Goal model and it's child campaign_roi?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们假设 4 种类型的模型包含无法合并为单一类型的信息。也就是说,它们没有任何共同点,只不过它们都有一个 goal_id 并且是互斥的。让我们假设这些模型中的任何一个的实例只能属于一个目标。
您可以使用“belongsTo”关系和条件子句来设置“Goal”模型,以便它将尝试加载每种类型的子级。
在您的目标表中,您将拥有 model 字段和 model_id 字段。模型字段将填充 CampaignRoi、CallSuccess、PhoneSkill 或 CallQuality。完成对 Goal 的查找后,您仍然需要检查 goal.model 以确定您需要处理什么类型的子项。
这是我已经使用过多次的方法,尽管我不太记得其他模型发生了什么。最坏的情况下,它是一个空数组或字段(即空($goal['CampaignRoi'])== true,如果$goal['Goal']['model'] == 'CallSuccess')。
关系数据库中的子类型从来都不是有趣的。
Let's assume that the 4 types of models hold information that can't be merged in to a single type. That is, they have no commonality, except that they all have a goal_id and are mutually exclusive. Let's assume that an instance of any one of these models can only belong to one goal.
You can set up your Goal model, using the belongsTo relationship, and the conditions clause, so that it will attempt to load a child of each type.
In your goal table, you'll have the model field and a model_id field. The model field will be populated with CampaignRoi, CallSuccess, PhoneSkill or CallQuality. After you've done your find on Goal, you'll still need to inspect goal.model to determine what type of child you need to work with.
This is a method I've used several times, though I can't quite remember what happens with the other models. At worst, it's an empty array or field (i.e. empty($goal['CampaignRoi']) == true, if $goal['Goal']['model'] == 'CallSuccess').
Sub-typing in relational databases is never fun.
对于模型继承,我们可以使用 main_table_id 创建 sub_table 以指向 main_table。但当你需要获得完整的记录时,这就相当复杂了。
如果这 4 个字段是孩子之间的唯一区别,您可以将它们全部放入目标表中。如果它们是相同的数据类型,则可以仅使用一个字段。您可以在模型中编写自定义查找方法(findModel1(),...)或定义一个数组('model1name'=>'campaign_roi',...),以便稍后在执行 find() 时轻松参考。
如何设计它实际上是健壮性/可扩展性和便利性之间的权衡。
For model inheritance, we can create sub_table with main_table_id to point back to the main_table. But it's quite convoluted when you need to get a full record.
If that 4 fields are the only difference between the children, you can put them all in the goals table. And if they are the same data type, you can use just one field. You can write custom find methods in the model (findModel1(),...) or define an array('model1name'=>'campaign_roi',...) for easy reference later when you do find().
How you design it is really a trade-off between robustness/extensibility and convenience.
你就不能反过来做吗?不是从目标的角度,而是从其他的角度?
不要在 Goal 模型中指定任何关系,而是在 CampaignRoi、CallSuccess、PhoneSkill 和 CallQuality 中指定 hasOne 关系。
通过 CampaignRoi、CallSuccess、PhoneSkill 和 CallQuality 进行查询。
Can't you work it the other way around? Not from the goal point of view, but from the others?
Don't specify no relation in the Goal model, specify hasOne relation in CampaignRoi, CallSuccess, PhoneSkill and CallQuality.
Do your queries thru CampaignRoi, CallSuccess, PhoneSkill and CallQuality.