从CakePHP中的一个表单将数据插入到两个表(外键连接)中 [英] Insert data into two tables (foreign key joined) from one form in CakePHP

查看:123
本文介绍了从CakePHP中的一个表单将数据插入到两个表(外键连接)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CakePHP中有一个表单如下
addticket.ctp

I have a form in CakePHP as following addticket.ctp

    <html>
<?php
    echo $this->Form->create('Ticket', array('url' => array('controller' => 'tickets', 'action' =>'addtickets'),
        'enctype' => 'multipart/form-data'));
    echo $this->Form->input('title',array('label'=>'Title'));
    echo $this->Form->input('attachment', array('between'=>'<br />','type'=>'file',

        'label'=>'Attachment'));
    echo $this->Form->input('stepstoreproduce',array('label'=>'Steps To Reproduce'));
    echo $this->Form->input('category',array(
        'label'=>'Category',
        'options'=>array(
            'IT Support',
            'IT HelpDesk'
            )));
    echo $this->Form->input('priority',array(
        'label'=>'Priority',
        'options'=>array(
            'Low',
            'Medium',
            'High'
            )));
    echo $this->Form->input('Comment.comment',array(
        'type'=>'textarea',
        'label'=>'Comments'
        ));
    echo $form->input('public',array('type'=>'radio',
    'options' => array(
        '1'=>'Yes',
        '0'=>'No',
    ),
    'default'=>'0'));
    echo $form->input('created_by',array('value'=>$_SESSION['Auth']['User']['id'],'type'=>'hidden'));

    echo $this->Form->end('Submit Ticket');

?>
</html>

我有ticket.php模型,带有以下代码

And I have model ticket.php with following code

var $hasMany = array(
    'Comment' => array(
        'className'     => 'Comment',
        'foreignKey'    => 'ticket_id'
        )
);

我有ticket_controller和addticket()函数,如下

And I have tickets_controller with addticket() function as follows

 if ($this->Ticket->saveAll($this->data)){
            $this->Session->setFlash('Ticket created');
        } 
        else {
            $this->Session->setFlash('Cannot create a ticket');
        }

问题
问题是在我的数据库中有两个表:
1.tickets
2.Comments(ticket_id是外键)

Problem The problem is that there are two tables in my database: 1.tickets 2.Comments (ticket_id is foreign key )

我想插入票据数据表和注释数据到注释表。由于我知道将名称更改为 Comment.comment 会在注释表中插入数据。

I want to insert tickets data to tickets table and comments data to comments table. As I know that changing name to Comment.comment will insert data in comments table.

但我想添加注释表中的 userid ticket_id
created_by 门票&

But I want to add userid and ticket_id in comment table and created_by in tickets & comments both.

请帮助
提前感谢

Please Help Thanks in advance

推荐答案

p>我认为你需要明确地在每个输入字段前面放置一个模型名称。例如:

I think you need to explicitly put a Model name in front of every input field you have. For example:

echo $this->Form->input('Ticket.title',array('label'=>'Title'));
echo $this->Form->input('Ticket.attachment', array('between'=>'<br />','type'=>'file',

    'label'=>'Attachment'));

CakePHP将在您的注释数据中自动设置ticket_id。
请参阅 http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveall-array-data-null-array-options-array for更多细节。

CakePHP will automatically set ticket_id in your Comment data. See http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveall-array-data-null-array-options-array for more detail.

我认为你应该在控制器中设置user_id,而不是设置为视图中的隐藏字段:

And I think you should set the user_id in the controller instead of set it as a hidden field in a view like:

$this->data['Ticket']['created_by'] = $this->Auth->user('id');
$this->data['Comment']['created_by'] = $this->Auth->user('id');

这篇关于从CakePHP中的一个表单将数据插入到两个表(外键连接)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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