0

codeigniterで私はいくつかのデータ挿入のための一般的な関数を作成しています

それのために私はいくつかの規範を守っています

1つはdt_categoryで、もう1つはdt_productである2つのテーブルがあるとします。

これがdt_category構造です

|-------------------|---------------------|--------------------|------------------|
| category_id       | category_name       | category_slug      |  timestamp       |
|-------------------|---------------------|--------------------|------------------|

これがdt_product構造です

|-------------------|---------------------|--------------------|------------------|
| product_id        |  product_name       |  product_slug      |  timestamp       |
|-------------------|---------------------|--------------------|------------------|

ご覧のとおり、両方のテーブルフィールドは、フィールド名のプレフィックスによって変更されます。つまり、dt_categoryではそのcategory_idを意味します。

dt_productではそのproduct_idなど

カテゴリデータを挿入する関数を呼び出している間、このメソッドを使用します

$category_name=$this->input->post('category_name');
               $insert_array=array('category_name'=>$category_name,
                                   'timestamp'=>time());
               $data['msg']=$this->erp_model->data_insert('dt_category',$insert_array);

製品データの場合は、この方法だけです

$product_name=$this->input->post('product_name');
               $insert_array=array('product_name'=>$product_name,
                                   'timestamp'=>time());
               $data['msg']=$this->erp_model->data_insert('dt_product',$insert_array);

挿入を処理する関数はこれです

function data_insert($table,$data)
    {
        $this->db->insert($table,$data);
        $timestamp=$data['timestamp'];
        $table_nm=explode('_',$table); //spliting the table name in format dt_table_name,
        $table_slug=$table_nm[1]."_"."slug";//if its dt_product table_nm[1]="product", 
                                            //if category,table_nm[1]="category"
                                            //thus $table_slug="product_slug" or "category_slug"
        $query = $this->db->select('*')
                        ->from($table)
                        ->where('timestamp', $timestamp)
                        ->get();
        foreach($query->result() as $r_q)
        {
            $id=$r_q->$table_nm[1].'_id';//i want $id value of product_id since $table_nm[1]="product"
            $name=$r_q->$table_nm[1].'_name';//i want $name value of product_name since $table_nm[1]="product"
        }
        echo $id."-".$name;


    }

table_nameに基づくaboce関数で、フィールド名の値を取得したい

$id=$r_q->$table_nm[1].'_id'; should resemble $id=$r_q->product_id; if the table is dt_product

文字列として表現しようとしているときに->product_idがresultanatオブジェクトであるため、間違った方法で実行していることはわかっています。

私が物事を達成する方法はありますか、

テーブル名に基づいてフィールド名を取得することを意味します。

4

1 に答える 1

0

わかりました、私は私の問題を解決しました、そしてここに答えがあります

検索時に連結を試していましたが、今は最初に連結してからフィールドを割り当てました

$tab_id=$table_nm[1].'_id';
            $tab_name=$table_nm[1].'_name';

これが解決されたコードです

function data_insert($table,$data)
        {
            $this->db->insert($table,$data);
            $timestamp=$data['timestamp'];
            $table_nm=explode('_',$table);
            $table_slug=$table_nm[1]."_"."slug";
            echo $table_slug;
            $query = $this->db->select('*')
                            ->from($table)
                            ->where('timestamp', $timestamp)
                            ->get();
            $tab_id=$table_nm[1].'_id';
            $tab_name=$table_nm[1].'_name';
            foreach($query->result() as $r_q)
            {
                $id=$r_q->$tab_id;
                $name=$r_q->$tab_name;
            }
            echo $id."-".$name;


        }
于 2013-03-27T15:22:05.677 に答える