当我使用其他表格的数据在cakephp中显示时,如何在表格中保存数据?

时间:2011-06-13 07:26:24

标签: cakephp

很抱歉,如果我的问题含糊不清,我试图保持尽可能简单,我在cakephp中使用以下控制器:

var $uses = array('Client','Ticket','Userseequeue','Queue','Task','User');
var $useTable = false;

function index() {
    $clientsResult = $this->Client->find('all');
    $userSeeQueuesResult = $this->Userseequeue->find('all', array('conditions' => array('Userseequeue.user_id' => '1') ) );
    $ticketsResult = $this->Ticket->find('all');
    $queuesResult = $this->Queue->find('all');
    $tasksResult = $this->Task->find('all', array('group' => array('Task.ticket_id','Task.queue_id') ) );
    $usersResult = $this->User->find('all');
    $this->set(compact('tickets','userSeeQueues','clients','queuesResult','tasksResult','users','ticketUsers','ticketQueues'));
    if (!empty($this->data)) {
        $this->Ticket->create();
        $this->Ticketsinqueue->insertData($this->data);
        if ($this->Ticket->save($this->data) && $this->Ticketsinqueue->save($this->data)) {
            $this->Session->setFlash(__('The ticket has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The ticket could not be saved. Please, try again.', true));
        }
    }
}

我使用这些表来显示单个视图的信息摘要。

我需要在该视图屏幕上添加一个表单以添加额外的票证。创建此表单并将其保存到表中,我做错了什么或缺少什么?

以下是我尝试创建表单的视图,以防它可能有所帮助:

<div class="Summary index">
<h2><?php __('Summary');?></h2>
<?php echo $this->Form->create('Ticket');?>
<fieldset>
    <legend><?php __('Add Ticket'); ?></legend>
<?php
    echo $this->Form->input('Ticket.name');
    echo $this->Form->input('Ticket.user_id');
    echo $this->Form->input('Ticket.queue_id');
?>
<?php echo $this->Form->end(__('Submit', true));?>
</fieldset>
...

这会创建表单,但它不会正确填充下拉列表中的下拉列表,就像在cakephp创建的普通添加视图中一样。它目前使用我在此控制器中使用的所有表填充下拉列表。

如果有人能帮助我或帮助我朝着正确的方向前进,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

我让这个工作:

让控制器在为视图创建变量之前先保存数据

并将每个下拉列表中的值列表保存为控制器中的变量。

var $uses = array('Client','Ticket','Userseequeue','Queue','Task','User');
var $useTable = false;

function index() {
    if (!empty($this->data)) {
        $this->Ticket->create();
        $this->Ticketsinqueue->insertData($this->data);
        if ($this->Ticket->save($this->data) && $this->Ticketsinqueue->save($this->data)) {
            $this->Session->setFlash(__('The ticket has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The ticket could not be saved. Please, try again.', true));
        }
    }
    $clientsResult = $this->Client->find('all');
    $userSeeQueuesResult = $this->Userseequeue->find('all', array('conditions' => array('Userseequeue.user_id' => '1') ) );
    $ticketsResult = $this->Ticket->find('all');
    $queuesResult = $this->Queue->find('all');
    $tasksResult = $this->Task->find('all', array('group' => array('Task.ticket_id','Task.queue_id') ) );
    $usersResult = $this->User->find('all');
    $ticketUsers = $this->User->find('list');
    $ticketQueues = $this->Ticket->Queue->find('list');
    $this->set(compact('tickets','userSeeQueues','clients','queuesResult','tasksResult','users','ticketUsers','ticketQueues'));
}

我的视图现在列出了我创建的控制器变量的值:

<div class="Summary index">
<h2><?php __('Summary');?></h2>
<?php echo $this->Form->create('Ticket');?>
<fieldset>
    <legend><?php __('Add Ticket'); ?></legend>
<?php
    echo $this->Form->input('Ticket.name');
    echo $this->Form->input('Ticket.user_id', array('options' => array($ticketUsers),'empty' => 'None'));
    echo $this->Form->input('Ticket.queue_id', array('options' => array($ticketQueues)));
?>
<?php echo $this->Form->end(__('Submit', true));?>
</fieldset>
...

有了这个,我可以在一页上添加一张票以及显示来自其他模特/表的信息。