CakePHP 多模型视图

发布于 2024-07-04 22:07:33 字数 643 浏览 4 评论 0原文

我正在 CakePHP 中创建一个网站,我对它有点陌生。 我在这个问题上找不到好的资源,所以就这样:

我有一个用于注册用户的三个表结构:UsersAddressesContacts. 我必须构建一个包含所有三个表信息的视图,例如:

Full Name:         [          ] (from Users)
Shipping Address:  [          ] (from Address)
Mobile Phone:      [          ] (from Contact)
e-Mail Address:    [          ] (from Contact)

处理这种情况的最佳方法是什么。 专门用于保存。 创建一个新模型来表示这一点,该模型本身将有一个 save() 方法(可能是数据库中的 sql 视图)创建一个控制器来处理这个 bind 的视图或 unbinds 信息

我仍然想知道如何处理这两个联系人,因为它们将是 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 binds or unbinds 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

从来不烧饼 2024-07-11 22:07:33

3 个模型:用户、地址、联系人

User hasOne Address, Contact
Address belongsTo User
Contact belongsTo User

在您的模型中定义如下:

class User extends AppModel {
var $name = 'User';
var $hasOne = array('Address','Contact');
..

要创建此视图,您需要 user_id 字段 ind 地址联系人 表格

要在视图中使用它,您只需在用户模型上调用一个查找,递归次数为 1(顺便说一句,用户控制器仅使用用户模型)。

$this->User->recursive = 1;
$this->set('user', $this->User->find('first', array('conditions'=>array('id'=>666)));

这将为您的视图带来这个数组:

array(
  'Use' => array(
     'id' => 666,
     'name' => 'Alexander'
),
  'Address' => array(
     'id' => 123,
     'zip' => 555
),
   'Contact' => array(
     'id' => 432,
     'phone' => '555-1515'
));

3 models : User, Address, Contact

User hasOne Address, Contact
Address belongsTo User
Contact belongsTo User

in your model you define this like this :

class User extends AppModel {
var $name = 'User';
var $hasOne = array('Address','Contact');
..

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->User->recursive = 1;
$this->set('user', $this->User->find('first', array('conditions'=>array('id'=>666)));

This will result in this array for your view :

array(
  'Use' => array(
     'id' => 666,
     'name' => 'Alexander'
),
  'Address' => array(
     'id' => 123,
     'zip' => 555
),
   'Contact' => array(
     'id' => 432,
     'phone' => '555-1515'
));
玩心态 2024-07-11 22:07:33

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.

风吹雪碎 2024-07-11 22:07:33

如果您使用最新的 1.2 代码,请查看 api 中的 Model::saveAll,

例如。 您的视图可能看起来像这样:

echo $form->create('User', array('action' => 'add');
echo $form->input('User.name');
echo $form->input('Address.line_1');
echo $form->input('Contact.tel');
echo $form->end('Save');

然后在您的用户控制器添加方法中,您将有类似的内容:

...

if($this->User->saveAll($this->data)) {
 $this->Session->setFlash('Save Successful');
 $this->redirect(array('action' => 'index'));
} else {
 $this->Session->setFlash('Please review the form for errors');
}

...

在您的用户模型中,您将需要类似的内容:

var $hasOne = array('Address', 'Contact');

希望有帮助!

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:

echo $form->create('User', array('action' => 'add');
echo $form->input('User.name');
echo $form->input('Address.line_1');
echo $form->input('Contact.tel');
echo $form->end('Save');

Then in your Users controller add method you'd have something like:

...

if($this->User->saveAll($this->data)) {
 $this->Session->setFlash('Save Successful');
 $this->redirect(array('action' => 'index'));
} else {
 $this->Session->setFlash('Please review the form for errors');
}

...

In your User model you will need something like:

var $hasOne = array('Address', 'Contact');

Hope that helps!

http://api.cakephp.org/class_model.html#49f295217028004b5a723caf086a86b1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文