0

codeigniter で開発したサイトがあり、テーブルを編集する関数を作成したいのですが、異なるフィールドでテーブルを更新する 2 つのページがあります。これはモデル内の私の機能です:

public function editHotel($service_id) {
         $hotel_id = $this->getHotelByServiceId($service_id);
         $hotel = array(
              'rating_id'=>$this->input->post('rating'),
              'treatment_id'=>$this->input->post('treatment'),
              'nation_id'=>$this->input->post('nation'),
              'region_id'=>$this->input->post('region'),
              'city_id'=>$this->input->post('city'),
              'area_id'=>$this->input->post('area'),
              'address'=>$this->input->post('address'),
              'phone'=>$this->input->post('phone'),
              'fax'=>$this->input->post('fax'),
              'email'=>$this->input->post('email'),
              'site'=>$this->input->post('site'),
              'description_it'=>$this->input->post('description_it'),
              'description_en'=>$this->input->post('description_en'),
              'latitudine_google'=>$this->input->post('latitudine_google'),
              'longitudine_google'=>$this->input->post('longitudine_google'),
              'modified'=> date('Y-m-d H:i:s'),
        );
        $this->db->where('service_id', $service_id);
        $this->db->update('hotel',$hotel);
    }

この関数では、すべてのテーブルを更新していますが、いくつかの値しかないページがあり、これだけを次のように更新したいと考えています。

public function editDescriptionHotel($service_id) {
             $hotel_id = $this->getHotelByServiceId($service_id);
             $hotel = array(
                  'description_it'=>$this->input->post('description_it'),
                  'description_en'=>$this->input->post('description_en'),
                  'modified'=> date('Y-m-d H:i:s'),
            );
            $this->db->where('service_id', $service_id);
            $this->db->update('hotel',$hotel);
        }

そして、テーブルの値を更新する他のページがあります。すべてのページで関数「editHotel」を使用すると、入力コードイグナイターがない場合、テーブルのフィールドに 0 が挿入されます 最初の関数「editHotel」のみを使用して、投稿したフィールドのみを更新することは可能ですか? すべてのフィールドではなく、ページにあるフィールドのみを更新する関数のみをすべてのページで使用したいと考えています。可能です?テーブルを更新するすべてのページで、テーブルの別の関数更新を書き込む必要はありません

4

1 に答える 1

2

入力フィールドがテーブルの列と同じ名前になるようにすれば、それは簡単です。

public function updateHotels($service_id) 
{
   $hotel_id = $this->getHotelByServiceId($service_id);
   $fields = array();

   foreach($this->input->post() as $k => $v){
    if($k != 'submit'){   //or anything you always want to esclude
      $fields[$k] = $v;
    }
   }

   $fields['modified'] = date('Y-m-d H:i:s');

   $this->db->where('service_id', $service_id);
   $this->db->update('hotel', $fields);
}


より細かく制御し、再利用性を高めるために、除外可能なインデックスの配列を引数として渡すことができます (列名を一致させたくない、または一致させたくない人)。さらに進んで、追加したい追加フィールドのリストを作成できます。

$exclude = array('submit', 'other_field');
$extra = array('modified' => date('Y-m-d H:i:s'));

public function updateHotels($service_id, $exclude, $extra) 
    {
       $hotel_id = $this->getHotelByServiceId($service_id);
       $fields = array();

       foreach($this->input->post() as $k => $v){
        if(!in_array($k, $exclude){
          $fields[$k] = $v;
        }
       }

       $columns = array_merge($fields, $extra);

       $this->db->where('service_id', $service_id);
       $this->db->update('hotel', $columns);
    }
于 2013-02-20T21:24:48.267 に答える