在Cakephp中烘焙代码时如何定义关联 [英] How associations get defined when code is baked in Cakephp

查看:46
本文介绍了在Cakephp中烘焙代码时如何定义关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在控制台中烘焙博客教程。我创建了一个名为posts的表,并烘焙了代码,所有MVC文件均已正确创建。然后,我创建了另一个名为注释的表,并再次烘焙了代码,但是烘焙没有定义任何关系,并且注释与博客分开创建。我要定义有很多关系,意味着博客有很多评论。如何通过控制台进行定义?请给点想法吗?还是我的烘烤不正确?非常感谢。

I was trying to bake the blog tutorial in console. I created one table named posts and baked the code and all MVC files were created properly. Then I created another table named comments and I again baked the code but no relationship gets defined by baking and comments was created as separate part from blog. I want to define has many relationship means blog has many comments. How will it get defined through console? Please give some idea? Or am I running bake incorrectly? Many thanks.

推荐答案

您可以重新烘焙模型,从而解决问题。但是最终您将无法做到。在理想的情况下,烘烤时所有桌子都在那里。但是通常情况会在以后发生变化。因此,您也应该能够手动添加关系,在这种情况下,这种关系可能会更快。

You could either re-bake your models, which will solve it. But eventually you won't be able to. In the ideal situation all the tables are there when you bake it. But usually things change later on. So you should be able to add relations manually as well, which in this case might be faster.

Post 模型。这是因为烘焙时,它会在其他表中检查引用新模型的外键。当表还不存在时,显然不会找到它。

The relation is probably missing in the Post model. This is because when baking, it checks other tables for foreign keys that refer to the new model. When the table is not there yet, it won't find it obviously.

我将在 Post.php

public $hasMany = array('Comment');

如果已经有 $ hasMany , '必须将其添加到现有数组中。

If there's already a $hasMany, you'll have to add it to the existing array.

这篇关于在Cakephp中烘焙代码时如何定义关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆