0

CodeIgniter モデルに関数があり、'active_date' レコード値を 'event_date' に単純に配置/コピーしたい (クエリと一致する同じ行)。「event_date」にストレート値を指定しようとしましたが、うまくいきました。クエリ結果でそれを行う方法がわかりません。ありがとう!

public function postx_model()
  {   
    $data = array(
      'last_status' => $this->input->post('status'),
      );

    if( $this->input->post('status') == 'Active' )
    {
       $this->db->select('active_date');
       $this->db->where('customer_code', $this->input->post('customer_code') );
       $query = $this->db->get('customer_table');

    $data2 = array(
    'event_date' => $query,
    //'event_date' => '2013-09-14', //this is work
      );

      $dataComplete = $data + $data2;
      $this->db->where('customer_code', $this->input->post('customer_code') );
      $this->db->update('customer_table', $dataComplete); 
    }
    else
    {
    $this->db->where('customer_code', $this->input->post('customer_code') );
    $this->db->update('customer_table', $data); 
    }
  }
4

4 に答える 4

0

このようなもの:

$query = $this->db->select('active_date')
->from('customer_table')
->where('customer_code', $this->input->post('customer_code'))
->get()->row_array();
$data2 = array('event_date' => $query['active_date']);
于 2013-09-04T08:11:32.953 に答える
0

観察:この行が何度も繰り返されていることに気づきましたか

  $this->input->post('customer_code') ; 

提案: データベースの挿入または更新内で $this->input->post などを使用しないでください。フィルター処理、クレンジング、検証が完了し、準備が整った変数または配列を渡すだけです。

  // adding TRUE enables xss clean 
  $customerCode = this->input->post('customer_code', TRUE) ;

任意のメソッドに $customerCode を使用できるようになりました。より明確で読みやすくなり、フォーム名が変更された場合でも、1 か所だけ変更する必要があります。おまけとして、xss クリーニングを追加しました。複数の方法で使用する必要がある場合:

 $this->customerCode = this->input->post('customer_code', TRUE) ;    

$this->customerCode は、クラス内の任意のメソッドで使用できるようになりました。

===== edit "クラス内の任意のメソッドで使用できるという意味は何ですか?" まあ -- これは $this-> の本当に素晴らしい点です。通常、メソッド (関数) で変数を設定すると、$myvariable になります。メソッド内でのみ使用できます。$this->myvariable を使用すると、クラス内の任意のメソッドですぐに利用できます。

これはクールですが、非常にクールなのは、モデルコンストラクターで $this->variable を設定できることです-データベーステーブルの名前、フィールドなど. $this->dbtable = 'acme_users'; $this->dbfields = 'id,first,last,email';

次に、メソッドで $this->dbtable、$this->dbfields などの汎用コードを使用できます。もう 1 つの利点は、モデルに関する非常に具体的な知識をコンストラクターの一番上に置くことができることです。それにより、作成するメソッド コードをより抽象的にすることができます。これにより、再利用/拡張が容易になります。

于 2013-09-04T20:31:14.517 に答える