开发者社区> 问答> 正文

CodeIgniter数据库规范化

首先,我首先使用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');
            }
        }
    }

这是我的看法

展开
收起
保持可爱mmm 2019-11-18 09:48:37 445 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载
云时代的数据库技术趋势 立即下载
超大型金融机构国产数据库全面迁移成功实践 立即下载