Codeigniter如何更新数据时,使用$this->db->set(Array)来阻止数组项null更新


Codeigniter how to update data when using $this->db->set( Array ) to block array item nulll to update

我想让我的代码干,我是新的编码器我使用$this->db->set($data),所以我不需要重复这个集合的许多,我怎么能防止$this->db->set($data)跳过in_image,如果它是空的设置成db时更新是可能的?

我的控制器:

$data_arr=array(
                        'title'=>$this->input->post('txttitle'),
                        'desc'=>$this->input->post('txtdesc'),
                        'addr'=>$this->input->post('txtaddr'),
                        'fbn'=>$this->input->post('txtfbname'),
                        'fburl'=>$this->input->post('txturl'),
                        'status'=>$this->input->post('status')
            );
            if(!empty($_FILES['File']['tmp_name']))
            {
                $new_file_name=date("mdY")."_".time();
                $config['upload_path']  = './assets/images/';
                $config['allowed_types']= 'gif|jpg|png';
                $config['max_size']     = 2048;
                $config['max_width']    = 640;
                $config['max_height']   = 420;
                $config['file_name']    = $new_file_name;
                $this->load->library('upload', $config);
                if ( ! $this->upload->do_upload('File'))
                {
                    $error = array('error' => $this->upload->display_errors());
                    $image_arr = array('in_image'=>'error');
                }
                else
                {
                    $image_arr = array('in_image'=>$this->upload->data('orig_name'));
                }
            }
            else
            {
                $image_arr = array('in_image'=>'');
            }
            $data_merge = array_merge($data_arr,$image_arr);
            $this->Description_model->update_all($data_merge,$id);

我的模型
public function update_all($data, $id)
{
    if($data['in_image'] != 'error' && empty($data['in_image'])
    {
        /*
          this is where i want to check if in_image is empty than prevent 
          $this->db->set($data) skip to set in_image
        */
        $this->db->set($data)
             ->where('in_id',$id);
        if($this->db->update('tbl_desc'))
        {return TRUE;}
        else
        {return FALSE;}
    }
    elseif($data['in_image'] != 'error' && $data['in_image'] != ''))
    {
    }
    else
    {return FALSE;}
}

提前致谢

你的帖子令人困惑,但似乎你想阻止更新每当in_image为空,正确吗?

$data = array
(
   'title'   => $this->input->post('txttitle'),
   'desc'    => $this->input->post('txtdesc'),
   'addr'    => $this->input->post('txtaddr'),
   'fbn'     => $this->input->post('txtfbname'),
   'fburl'   => $this->input->post('txturl'),
   'status'  => $this->input->post('status')
);
if(!empty($_FILES['File']['tmp_name']))
{
   $new_file_name           = date("mdY")."_".time();
   $config['upload_path']   = './assets/images/';
   $config['allowed_types'] = 'gif|jpg|png';
   $config['max_size']      = 2048;
   $config['max_width']     = 640;
   $config['max_height']    = 420;
   $config['file_name']     = $new_file_name;
   $this->load->library('upload', $config);
   if($this->upload->do_upload('File'))
      $data['in_image'] = $this->upload->data('orig_name');
}
/*
 * Checks if the `in_image` index exists in the array.
 * Only if the index exists is when it allows the update to happen.
 */
if(isset($data['in_image']) == TRUE)
   $this->Description_model->update_all($data, $id);

现在在你的模型中你所需要的是:

public function update_all($data, $id)
{
   $this->db->where('id', $id)
            ->update('table_name', $data);
}

在codeigniter中,数据更新主要有两种方式,

$data  = array{
          'YOUR_COL_1' => 'YOUR_VALUR_1',
          'YOUR_COL_2' => 'YOUR_VALUR_2',
};
$this->db->where('id', $id);
$this->db->update('YOUR_TABLE', $data);

$this->db->set('YOUR_COL_1', 'YOUR_VALUR_1');
$this->db->set('YOUR_COL_2', 'YOUR_VALUR_2');//if 2 columns
$this->db->where('id', $id);
$this->db->update('YOUR_TABLE');

在if条件和!empty

中还需要另一个右括号
if($data['in_image'] != 'error' && !empty($data['in_image']))