我有三张表:电脑有很多品牌,品牌属于电脑和零件。现在我的品牌description
、computer_id
、part_id
中有这些字段。我有下面的代码来保存我的数据。它将保存描述和part_id。。。。但是我的computer_id根本不保存
通常我的URL写为http://192.168.6.253/computers/brands/add/1
,其中1是computer_id。
我将如何保存它?我还是这个框架的初学者
控制器
public function add($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
//Assign value to link computer_id
$data = $this->Brand->Computer->findById($id);
$this->set('computers', $data);
//assign select values to parts select
$this->set('parts', $this->Brand->Part->find('list',
array('fields' => array('description'))));
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' => 'table/'.$id));
} else {
$this->Session->setFlash(__('Unable to add your post.'));
}
}
}
查看
<?php
echo $this->Form->create('Brand');
echo $this->Form->input('part_id',array('empty'=>' '));
echo $this->Form->input('description');
echo $this->Form->input('computer_id',array('type'=>hidden));
echo $this->Form->end('Save Post');
?>
如果您不需要通过表单发送计算机id,因为它是一个url参数。。。你可以像这个一样调整你的添加功能
if ($this->request->is('post')) {
$this->request->data['Brand']['computer_id'] = $id; //manually add the id to the request data object
$this->Brand->create();
if ($this->Brand->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
$this->redirect(array('action' => 'table/'.$id));
} else {
$this->Session->setFlash(__('Unable to add your post.'));
}
}
这是一种非常基本的方法,无需检查数据完整性,任何人都可以轻松地将参数更改为2,但它符合您当前的设置。