如何创建一个视图页面来保存cakePHP中的两个对象 [英] How to create a view page to save two objects in cakePHP

查看:166
本文介绍了如何创建一个视图页面来保存cakePHP中的两个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型


地址:firstline,secondline
联系人:firstname,lastname

Address:firstline,secondline Contact:firstname,lastname

和数据库表的地址和联系人。我必须创建一个视图页面,应该显示如下格式

and database tables address and contact respectively. i have to create a view page which should display like below format

名字:
姓氏:

first name: last name:

地址:
第一行:
第二行:

Address: First line: second Line:

下一步{此按钮转到下一页}

NEXT STEP {this button leads to next page}

如何创建这个view.please用代码
解释我如何在单击下一步按钮后在会话中保存对象,以及要写入的控制器是什么。解释我的代码。

How to create this view.please explain me with the code and how to save both the objects in the session after clicking on next step button.and what are the controllers to be written.explain me with the code.

推荐答案

address_controller.php

address_controller.php

class AddressController extends AppController {   
 var $uses = array('Address', 'Contact');

以上操作将允许您从地址控制器访问联系人模型

the above will allow you access to the Contact Model from the Address Controller

那么你可以将你的add函数中的数据保存到两个表中

then you can save the data from your add function into both tables

$addressData = array(
    'id'=>'', 
    'firstline'=>$this->data['Address']['firstline'], 
    'secondline'=>$this->data['Address']['secondline'], 
    'contact_id'=>$this->Auth->user('id')
);

然后使用以下代码保存数据....

Then save the data with the following code....

$this->Address->save($addressData);

我建议您进行检查,以确保数据实际保存,

I would recommend that you do a check to make sure that the data actually saved and then proceed onto the normal save method for the Contact model.

如果您已经为地址和联系人模型创建了add.ctp,那么您只需转到您的视图 - >地址 - > add.ctp并复制表单字段并将其粘贴到views-> contact-> add.ctp表单中。

if you already have the add.ctp created for the Address and Contact Models, then you can just go to your views->address->add.ctp and copy the form fields and paste them into the views->contact->add.ctp form.

没有看到任何代码,要准确地告诉你的字段,但应该看起来像这样:

without seeing any of your code, it's hard to tell exactly what your fields will be but should look something like this :

<input name="data[Contact][firstname]">
<input name="data[Contact][lastname]">
<input name="data[Address][firstline]">
<input name="data[Address][secondline]">

这篇关于如何创建一个视图页面来保存cakePHP中的两个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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