使用不同的模型将记录从元素传递到视图

时间:2013-02-22 10:52:08

标签: cakephp

我有一个应用程序,用户可以添加他们的查询(ItQueries / add)。当IT部门查看查询(ItQueries / view / 1)时,他们应该能够对其进行评论/回复。到目前为止我所做的是。

在IT查询的视图中,我已经为IT部门调用了一个元素来添加响应。下面是我如何调用元素

<?php echo $this->Element('/ItQueryResponses/add'); ?>

在提交ItQueryResponses时,我希望将操作重定向到ItQueris的视图页面,并在其下面添加注释。

这是我的元素代码

<?php echo $this->Form->create('ItQueryResponse', array('action' => 'add')); ?>
<?php echo __('Add It Query Response'); ?>
<?php
echo $this->Form->input('it_query_id');
echo $this->Form->input('response');
?>
<?php echo $this->Form->end(__('Submit')); ?>

这是我对ItQueryResponseController的添加功能

public function add() {
if ($this->request->is('post')) {
$this->ItQueryResponse->create();
if ($this->ItQueryResponse->save($this->request->data)) {
$this->Session->setFlash(__('The it query response has been saved'));
$this->redirect(array('Controller' => 'ItQueryResponses', 'action' => 'view'));
} else {
$this->Session->setFlash(__('The it query response could not be saved. Please, try again.'));
}
}
}

这是我的ItQueryResponses视图的代码

<h2><?php  echo __('It Query Response'); ?></h2>
<dl>
<dt><?php echo __('Response'); ?></dt>
<dd>
<?php echo h($itQueryResponse['ItQueryResponse']['response']); ?>
        &nbsp;
</dd>
<dt><?php echo __('Created'); ?></dt>
<dd>
<?php echo h($itQueryResponse['ItQueryResponse']['created']); ?>
        &nbsp;
</dd>
</dl>

下面是我的ItQueriesController

<?php
App::uses('AppController', 'Controller');

class ItQueriesController extends AppController {

/** index method

public function index() {
$this->ItQuery->recursive = 0;
$this->set('itQueries', $this->paginate());
}

/** view method

public function view($id = null) {
$this->ItQuery->id = $id;
if (!$this->ItQuery->exists()) {
throw new NotFoundException(__('Invalid it query'));
}

$this->set('itQuery', $this->ItQuery->read(null, $id));   
}

/ **添加方法

public function add() {
if ($this->request->is('post')) {
$this->ItQuery->create();
if ($this->ItQuery->save($this->request->data)) {
$this->Session->setFlash(__('The it query has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The it query could not be saved. Please, try again.'));
}                       
}
$hrEmployees = $this->ItQuery->HrEmployee->find('list');
$this->set(compact('hrEmployees'));
}

/**OTHER CODE*/
}

如果有人可以告诉我如何重定向到ItQueryResponses视图并在视图下方放置ItQueryResponse,这将是非常棒的。

1 个答案:

答案 0 :(得分:0)

/** view method **/

public function view($id = null) {
$this->ItQuery->id = $id;
if (!$this->ItQuery->exists()) {
throw new NotFoundException(__('Invalid it query'));
}

// debugging
$this->ItQuery->recursive = '1';
pr($this->ItQuery->find('all'));
//end debugging

$this->set('itQuery', $this->ItQuery->read(null, $id));   
}

然后导航到视图并显示输出:

会是这样的:

Array(
   ItQuery => array(
      id => 1
      ...
   )
   ItQueryResponse => array(
      0 => array(
          id =>3
          response => 'some response'
          ...
      )
      1 => array(
          id =>4
          response => 'some other response'
          ...
      )
   )
)