将信息保存在“sub”中 CakePHP 中的模型
我有一个简单的 CakePHP 站点 (1.2)
。 我有一个页面,您可以在其中编辑和保存人员。 所以我有一个 Person 模型和控制器。
在评论表中,每个人都没有或有更多评论。 所以我有一个 Comment 模型,并且我的 Person 模型与 Comment 模型之间有一个 hasMany 关联。 View 运行良好。
我的问题是,在查看“人员”页面上,我有一个添加评论按钮。 这应该如何运作? 我是否应该期望人员控制器包含评论记录的保存,或者创建一个评论控制器并将其保存在人员关联之外?
我对 PHP 有丰富的经验,但对 Cake 来说还是个新手。
有任何想法吗? 我想我只是错过了一些明显的事情,但我不知道该怎么做。 我觉得如果这是 PHP
我会在添加评论表单中引用 Person_id,从而使用单独的控制器,但我觉得为简单模型提供控制器是没有用的,因为评论只是在人员记录的上下文中进行编辑。
有想法吗?
I've got a simple CakePHP site (1.2)
. I've got a page where you can edit and save a Person. So I have a Person model and controller.
Each Person has none or more comments, in the comment table. So I have a Comment model, and I have a hasMany association on my Person model to the Comment model. View is working great.
My question is, on the view Person page, I have an add comment button. How should this work? Should I expect the Person controller to include a save for the comment record, or create a comment controller and save it outside of it's association for a person?
I'm experienced with PHP, but brand new to Cake.
Any ideas? I think I'm just missing something obvious, but I'm not sure what to do. I feel like if this was PHP
I would reference the Person_id in my add comment form, and thus use a separate controller, but I feel like having a controller for a simple Model is useless, since Comments are only edited in the context of a Person record.
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不是 CakePHP 专家,但我仍然认为拥有自己的控制器是有意义的。 我记得在做其中一个 CakePHP 博客教程时,您需要在评论模型中链接评论和帖子。 这是我从中得到的一些代码:
然后你需要一个控制器(comments_controller.php):
一些 SQL:
$scaffold
为你创建一个 CRUD 应用程序,所以当你转到 /comments 时在浏览器中,您可以创建创建、阅读、u更新和删除评论。 所以,如您所见,这里没有太多涉及。 您所需要的只是数据库表和一些提供 person_id 的逻辑。保存评论(在您的 Person/view 中):
在您的 CommentsController 中:
所以您基本上覆盖了 Cake 本身添加的标准添加操作。 希望现在这是有道理的。 另外,您可能需要一条路线,以便它获取 /comments/add/ID。 我不知道这部分。 :)
I'm not a CakePHP expert, but I still think it would make sense to have your own controller. From what I remember from doing one of those CakePHP blog tutorials is, that you need to link the comments and the post in the comment model. This is some of the code I have from it:
And then you need a controller (comments_controller.php):
Some SQL:
The
$scaffold
creates a CRUD application for you, so when you go to /comments in your browser you can create, read, update and delete comments. So, as you see, there is not much involved here. All you need is your database tables and a little logic to provide person_id.To save a comment (in your Person/view):
And in your CommentsController:
So you basically overwrite the standard add action, which Cake adds by itself. Hope that makes sense now. Also, you might need a route so it picks up /comments/add/ID. I don't know about this part. :)