How associations get defined when code is baked in Cakephp

孤街醉人 提交于 2020-01-05 14:04:02

问题


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.


回答1:


You need to re-bake your posts model for this to get correctly




回答2:


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.

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.

I would add the following code in Post.php:

public $hasMany = array('Comment');

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



来源:https://stackoverflow.com/questions/20993579/how-associations-get-defined-when-code-is-baked-in-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!