CakePHP即使在更改视图时也会维护ID


CakePHP maintain ID even when changing views

如何将传递的表单id保存到另一个表单?例如,我具有CCD_ 1,其显示来自条件computer_id=4的计算机的品牌的所有记录。现在我有了add(),它将我引导到http://192.168.6.253/computers/brands/add

我现在的问题是,我想保留computer_id=4,这样当我添加新品牌时,它会将其保存到DB中的brand.computer_id。所以我也想要类似http://192.168.6.253/computers/brands/add/4的东西。

以下是我如何在视图中调用add()

echo $this->Html->link('Add Brands Here', array(
        'controller' => 'brands',
        'action' => 'add'))
);

这里我如何称我的桌子品牌也在我的电脑视图

echo $this->Html->link('P',array('action' => '../brands/table', $computer['Computer']['id']));

我的品牌add()和table()控制器

public function table($id = null){
            if (!$id) {
                throw new NotFoundException(__('Invalid post'));
            }
            $this->paginate = array(
            'conditions' => array('Brand.computer_id' => $id),
            'limit' => 10
            );
            $data = $this->paginate('Brand');
            $this->set('brands', $data);
        }
        public function add() {
            if ($this->request->is('post')) {
                $this->Brand->create();
                if ($this->Brand->save($this->request->data)) {
                    $this->Session->setFlash(__('Your post has been saved.'));
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('Unable to add your post.'));
                }
            }
        }
echo $this->Html->link('Add Brands Here'
    , array(
      'controller' => 'brands'
      ,  'action' => 'add'
      , 4 // or whatever variable, maybe $computer['Computer']['id'] ?
    )
);

应该做到这一点。

它与您已经用来创建另一个链接的片段完全相同。echo $this->Html->link('P',array('action' => '../brands/table', $computer['Computer']['id']));

在最后制作这些数字"id"时要记住的关键点是,只需将一个未索引的项添加到数组中。CakePHP会把它们钉到最后。

当然,我还必须警告您,从REST的角度来看,在末尾添加"4"是没有意义的。也许你最好使用命名参数,比如这样。。。

echo $this->Html->link('Add Brands Here'
    , array(
      'controller' => 'brands'
      ,  'action' => 'add'
      ,  'computer_id' => 4 // or whatever variable, maybe $computer['Computer']['id'] ?
    )
);

或查询字符串参数。。。

echo $this->Html->link('Add Brands Here'
    , array(
      'controller' => 'brands'
      ,  'action' => 'add'
      ,  '?' => array('computer_id' => 4) // or whatever variable, maybe $computer['Computer']['id'] ?
    )
);

更深入地阅读http://book.cakephp.org/2.0/en/development/routing.html