首先,我首先使用JSON数据类型进行实验,只是看它是否要存储多个值,然后才发现它影响了数据库中的某些表。我现在要做的是为数据库实现规范化,我需要帮助来实现这一点。如何更新数据库和规范化代码?这是我以前的问题,是否可以在mysql数据库的一列中发布两个数据?与此有关。
这是我目前的桌子。 在此处输入图片说明 这就是我想做的一个例子 在此处输入图片说明
这是我的模型的代码
class Systemunit_model extends CI_Model{ function create($data){ $this->db->trans_start(); $add = $this->db->insert('system_unit', $data);
$insert_id = $this->db->insert_id();
if($add){
$ram = explode(",", $data['ram']);
$r = count($ram);
$hdd = explode(",", $data['hdd']);
$h = count($hdd);
$lan = explode(",", $data['lan_card']);
$l = count($lan);
$monitor = explode(",", $data['monitor']);
$m = count($monitor);
$set = array(
'quantity'=>0,
'status'=>'DEPLOYED',
'location_id'=>$data['location_id'],
'su_id'=>$insert_id
);
$this->db->set($set);
$this->db->where('item_id', $data['motherboard']);
$this->db->or_where('item_id', $data['processor']);
for($a=0; $a<$r;$a++){
$this->db->or_where('item_id', $ram[$a]);
}
for($b=0; $b<$h;$b++){
$this->db->or_where('item_id', $hdd[$b]);
}
$this->db->or_where('item_id', $data['video_card']);
for($c=0; $c<$l;$c++){
$this->db->or_where('item_id', $lan[$c]);
}
for($d=0; $d<$m;$d++){
$this->db->or_where('item_id', $monitor[$d]);
}
$this->db->or_where('item_id', $data['power_supply']);
$this->db->update('items');
}
//
$this->db->trans_complete();
这是我的控制器
function add(){ $this->form_validation->set_rules('location_id', 'Location', 'is_unique[system_unit.location_id]', array('is_unique'=>'This location already has a unit.'));
if($this->form_validation->run() == FALSE){
redirect('systemunits');
}else{
$id = $this->input->post('su_id');
$location_id = $this->input->post('location_id');
$this->db->where('location_id', $location_id);
$query = $this->db->get('locations');
foreach($query->result() as $row){
$shorthand = $row->shorthand;
}
$unit = strtolower($shorthand);
if($id){
$data = array(
'unit'=>$unit,
'motherboard'=>$this->input->post('motherboard'),
'processor'=>$this->input->post('processor'),
'ram'=>json_encode(implode(",", $this->input->post('ram'))),
'hdd'=>json_encode(implode(",", $this->input->post('hdd'))),
'video_card'=>$this->input->post('video_card'),
'lan_card'=>json_encode(implode(",", $this->input->post('lan_card'))),
'monitor'=>json_encode(implode(",", $this->input->post('monitor'))),
'power_supply'=>$this->input->post('power_supply'),
'location_id'=>$location_id,
'comment'=>$this->input->post('comment')
);
$result = $this->systemunit_model->update($data, $id);
if($result > 0){
$this->session->set_flashdata('success', 'Update successfull!');
}else{
$this->session->set_flashdata('error', 'Failed to update!');
}
redirect('systemunits');
}else{
$data = array(
'unit'=>$unit,
'motherboard'=>$this->input->post('motherboard'),
'processor'=>$this->input->post('processor'),
'ram'=>json_encode(implode(",", $this->input->post('ram'))),
'hdd'=>json_encode(implode(",", $this->input->post('hdd'))),
'video_card'=>$this->input->post('video_card'),
'lan_card'=>json_encode(implode(",", $this->input->post('lan_card'))),
'monitor'=>json_encode(implode(",", $this->input->post('monitor'))),
'power_supply'=>$this->input->post('power_supply'),
'location_id'=>$location_id,
'comment'=>"none"
);
$result = $this->systemunit_model->create($data);
if($result > 0){
$this->session->set_flashdata('success', 'Peripheral added!');
}else{
$this->session->set_flashdata('error', 'Failed to add peripheral!');
}
redirect('systemunits');
}
}
}
这是我的看法
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。