2

コントローラから呼び出されているモデル更新関数があります。

function update_customer_records( $id, $data )
{
    $this->db->where( 'id', $id );
    $this->db->update( 'customers', $data );
}

テーブルを更新しているだけです。更新が失敗した場合、重複する顧客名の例[一意のフィールド]flashdataをユーザーに送り返したい$this->session->set_flashdata('dbaction', 'Update Failed, possible duplicate Customer Name. Please try again or contact the administrator');

だからこのようなもの:

function update_customer_records( $id, $data )
{
    $this->db->where( 'id', $id );
    $this->db->update( 'customers', $data );

    if(update fails){
    $this->session->set_flashdata('dbaction', 'Update Failed, possible duplicate Customer. Please try again or contact the administrator');
    redirect('masterdata/create_customer', 'refresh');
    } else
    {
    $this->session->set_flashdata('dbaction', 'Update Successful');
    redirect('masterdata/create_customer', 'refresh');  
    }
}

これは許容できるでしょうか、それともこれを処理するためのより良い方法がありますか?

いつものように、事前に感謝します。

4

2 に答える 2

4

あなたはこれを使うことができます。更新の影響を受ける行が 1 より大きい場合は true を返し、それ以外の場合は false を返します

function update_customer_records( $id, $data)
{
    $this->db->where( 'id', $id );
    $this->db->update( 'customers', $data );

     //if affected rows > 0 reutrn true else false
    return $this->db->affected_rows() > 0 ? TRUE : FALSE;
}

次に、コントローラーで次のように使用できます

if($this->model->update_customer_records() == FALSE)
{
   // set the flashdata here

}else{

  // do what you want if update is successful
}
于 2013-03-04T07:32:17.840 に答える
1

これをモデルで使用できます。

function update_customer_records( $id, $data ) {    
            $this->db->where( 'id', $id );
            if($this->db->update( 'customers', $data ))
              return true;
            else
              return false;
        }



function is_exist($customer_name)
    {
        $query = $this->db->get_where($customers, array('customer_name' => $customer_name), 1);
        if ($query->num_rows() > 0)
            return false;
        else
           return true; 
    }

そしてコントローラーでこれを使用します:

if($this->model->is_exist($customer_name)
    {
        $this->db->update_customer_records( $id, $data ); 
        $this->session->set_flashdata('dbaction', 'Update Successful');
    }
else
   {
       $this->session->set_flashdata('dbaction', 'Update Failed, possible duplicate  
       Customer. Please try again or contact the administrator');     
    }
    redirect('masterdata/create_customer', 'refresh'); 
于 2013-03-04T07:31:01.493 に答える