如何在代码点火器中将某个表的 id 插入到另一个表中


How to insert id of some table into another in codeigniter

我正在插入第一个表中的 2 个表中 im 插入此数组

   array(2) {
  [0]=>
  array(5) {
    ["language_id"]=>
    string(1) "3"
    ["property_id"]=>
    int(82)
    ["option_id"]=>
    string(2) "10"
    ["value"]=>
    string(0) ""
    ["value_num"]=>
    NULL
  }

在第二个 im 中插入此数组

array(2) {
["property_type_id"]=>
string(2) "21"
["alt_txt"]=>
string(7) "4666.45"}

这是我在控制器中插入的功能

    public function save_dynamic($data, $id)
{
    // Delete all
    $this->db->where('property_id', $id);
    $this->db->where('value !=', 'SKIP_ON_EMPTY');
    $this->db->delete('property_value'); 
    // Insert all
    $insert_batch = array();
    foreach($data as $key=>$value)
    {
        if(substr($key, 0, 6) == 'option')
        {
            $pos = strpos($key, '_');
            $option_id = substr($key, 6, $pos-6);
            $language_id = substr($key, $pos+1);
            $val_numeric = NULL;
            if( is_numeric($value) )
            {
                $val_numeric = intval($value);
            }
            $insert_arr = array('language_id' => $language_id,
                                'property_id' => $id,
                                'option_id' => $option_id,
                                'value' => $value,
                                'value_num' => $val_numeric);
            if($value != 'SKIP_ON_EMPTY')
                $insert_batch[] = $insert_arr;

        }
    }
                        $data=$this->input->post('data');
    if(count($insert_batch) > 0)
        $this->db->insert_batch('property_value', $insert_batch); 
    if($this->db->affected_rows() > 0)
        $this->db->insert_batch('property_type_details', $data);

这是我的表格的样子

Field Type
id int(11) NOT NULL
language_id int(11) NOT NULL
property_id int(11) NOT NULL
option_id int(11) NOT NULL
value text NULL
value_num int(11) NULL
Field Type
id int(10) unsigned NOT NULL
property_id int(10) NULL
property_type_id int(10) NULL
alt_txt double(10,2) NULL

所以我想做的是将property_id与第二个数组一起插入第二个表中

使用 codeigniter 返回$this->db->insert_id()或 $get_id = $this->db->insert_id

这是我从另一个表中获取 id 的方法:

public function addBanner() {
    $data = array('name' => $this->input->post('name'),'status' =>   $this->input->post('status'));
    $this->db->set($data);
    $this->db->insert_id();
    $this->db->insert('banner');
    $banner_id = $this->db->insert_id();
    $data = array(
        'banner_id' => (int)$banner_id,
        'image' => $data_image['file_name'],
        'link' => $link
    );

    $this->db->set($data);
    $this->db->insert_id();
    $this->db->insert('banner_image');
    }
}
return $banner_id;
}
public function save_dynamic($data, $id)
{
    // Delete all
    $this->db->where('property_id', $id);
    $this->db->where('value !=', 'SKIP_ON_EMPTY');
    $this->db->delete('property_value'); 
    // Insert all
    $insert_batch = array();
    foreach($data as $key=>$value)
    {
        if(substr($key, 0, 6) == 'option')
        {
            $pos = strpos($key, '_');
            $option_id = substr($key, 6, $pos-6);
            $language_id = substr($key, $pos+1);
            $val_numeric = NULL;
            if( is_numeric($value) )
            {
                $val_numeric = intval($value);
            }
            $insert_arr = array('language_id' => $language_id,
                                'property_id' => $id,
                                'option_id' => $option_id,
                                'value' => $value,
                                'value_num' => $val_numeric);
            if($value != 'SKIP_ON_EMPTY')
                $insert_batch[] = $insert_arr;

        }
       $data=$this->input->post('data');
       $this->db->insert('property_value', $insert_batch);
       $data['property_id']=$this->db->insert_id();
       $this->db->insert('property_type_details', $data);
    }
 }