引用另一列子串后自动填充sql列数据

Automatically filling in sql column data after referencing a substring of another column

目前我正在使用 Mysql 和 CodeIgniters MVC 框架来填充我的数据。 table 记录每一次状态改变以及改变的时间。这是数据库当前的样子:

我在 table 中添加了名为 status_from 和 status_to 的新列,我希望这些列从操作列中获取子字符串。

但现在如何使用以下代码将其显示在我的数据库中:

控制器class:

public function status($status){ 
        $statusar = array('D'=>'Draft','N'=>'Unpublish','Y'=>'Publish','U'=>'Action','L'=>'Unlisted','S'=>'Sold','T'=>'Let');
        if($this->input->post('id')){
            foreach($this->input->post('id') as $key => $id):
            $check = $this->listings_model->loadlisting_check($id);
            $log = "Listing website status changed from ". $statusar[$check->status]." to ".$statusar[$status].". The listing ID is #".$id.".";
            $this->logs_model->insert_log(array('refno'=>$check->refno,'action'=>trim($log)));
            $data=array('status'=>$status);  
            if($status == 'T' || $status == 'Y' || $status == 'S'){
                $pub = 1;
            }else{
                $pub =0;
            }
            $this->listings_model->lpupdate(array('property_publish'=>$pub),$id);

            endforeach; 
        } 
        return true;
    }

listings_model:

function loadlisting_check($id)
    {
        $this->db->select("refno, status, archive");
        $id=$this->db->escape_str($id);
        $cond=array("$this->table_name.$this->primary_key"=>$id);
        $this->db->where($cond); 
        $this->db->from($this->table_name);
        $query = $this->db->get();
        return $query->row();
     } 

logs_model:

public function insert_log($log)
    {
        $log['agent_id'] = $this->session->userdata('clientsessuserid');
        $log['logtime'] = date('Y-m-d H:i:s');
        $this->db->insert($this->table_name, $log);
        return true;
    }

基本上我想在 status_from 列中填充 $statusar[$check->status] 并在 status_to 列中填充 $statusar[$status] 当每个新条目被创建时

您可以在 insert_log 方法参数中传递要添加到数组中的变量,并在 table[ 中使用与列名称匹配的适当键=11=]

 public function status($status){ 
            $statusar = array('D'=>'Draft','N'=>'Unpublish','Y'=>'Publish','U'=>'Action','L'=>'Unlisted','S'=>'Sold','T'=>'Let');
            if($this->input->post('id')){
                foreach($this->input->post('id') as $key => $id):
                $check = $this->listings_model->loadlisting_check($id);
                $log = "Listing website status changed from ". $statusar[$check->status]." to ".$statusar[$status].". The listing ID is #".$id.".";
                $this->logs_model->insert_log(array('refno'=>$check->refno,'action'=>trim($log) , 'status_from'=>$statusar[$check->status] ,'status_to'=>$statusar[$status]));
                $data=array('status'=>$status);  
                if($status == 'T' || $status == 'Y' || $status == 'S'){
                    $pub = 1;
                }else{
                    $pub =0;
                }
                $this->listings_model->lpupdate(array('property_publish'=>$pub),$id);
    
                endforeach; 
            } 
            return true;
        }

而且您不需要向模型中添加任何其他内容