CakePHP 多模型视图
我正在 CakePHP 中创建一个网站,我对它有点陌生。 我在这个问题上找不到好的资源,所以就这样:
我有一个用于注册用户的三个表结构:Users
、Addresses
和 Contacts. 我必须构建一个包含所有三个表信息的视图,例如:
Full Name: [ ] (from Users) Shipping Address: [ ] (from Address) Mobile Phone: [ ] (from Contact) e-Mail Address: [ ] (from Contact)
处理这种情况的最佳方法是什么。 专门用于保存。 创建一个新模型来表示这一点,该模型本身将有一个 save()
方法(可能是数据库中的 sql 视图)创建一个控制器来处理这个 bind
的视图或 unbind
s 信息
我仍然想知道如何处理这两个联系人,因为它们将是 2 个不同的 INSERT
's
任何我可以挖掘的提示或资源我都会很高兴。
I am creating a website in CakePHP and I am kind of new on it. I couldn't find good resources on this matter, so there you go:
I have a three table structure for registering users: Users
, Addresses
and Contacts
. I have to build a view with info of all three tables like:
Full Name: [ ] (from Users) Shipping Address: [ ] (from Address) Mobile Phone: [ ] (from Contact) e-Mail Address: [ ] (from Contact)
What is the best way to deal with this situation. Specially for saving. Creating a new Model to represent this, that will have a save()
method itself (Maybe a sql view in the database) Create a Controller to deal with this View that bind
s or unbind
s info
I wonder still how I will handle both contacts as they will be 2 different INSERT
's
Any hint or resources I can dig of I will be glad.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的模型中定义如下:
要创建此视图,您需要 user_id 字段 ind 地址 和 联系人 表格
要在视图中使用它,您只需在用户模型上调用一个查找,递归次数为 1(顺便说一句,用户控制器仅使用用户模型)。
这将为您的视图带来这个数组:
in your model you define this like this :
To make this view, you need user_id field ind addresses, and contacts tables
To use this in a view, you simply call a find on the User model with a recursive of one (and btw, the users controller only uses User model).
This will result in this array for your view :
CakePHP 允许您使用关系轻松维护模型之间的链接,请参阅 http: //book.cakephp.org/view/78/Associations-Linking-Models-Together 了解完整的详细信息。 然后检索正确的用户,您还将“免费”获得其地址和联系信息。
CakePHP allows you to easily maintains link between your models using relationship, see http://book.cakephp.org/view/78/Associations-Linking-Models-Together for the complete detail. Then retreiving the right User, you'll also get "for free", its address and contact informations.
如果您使用最新的 1.2 代码,请查看 api 中的 Model::saveAll,
例如。 您的视图可能看起来像这样:
然后在您的用户控制器添加方法中,您将有类似的内容:
在您的用户模型中,您将需要类似的内容:
希望有帮助!
http://api.cakephp.org/class_model.html#49f295217028004b5a723caf086a86b1
If your using the latest 1.2 code, check out Model::saveAll in the api
eg. Your view might look something like this:
Then in your Users controller add method you'd have something like:
In your User model you will need something like:
Hope that helps!
http://api.cakephp.org/class_model.html#49f295217028004b5a723caf086a86b1