dublicate a child table rows and parent table rows mysql [CodeIgniter]


    // call to model to model ...
    public function duplicateTableRecord ($table, $keys = [],$ignore_key) 
    {
       /* generate the select query */
       if (is_array($keys)) {
           foreach ($keys as $key_field => $key_val) {

               $this->db->where($key_field, $key_val); 
           
           }
       }
       // return var_dump($keys);
       $query = $this->db->get($table);
      
        foreach ($query->result() as $row){   
           foreach($row as $key=>$val){        
              if($key != $ignore_key){ 
              /* $this->db->set can be used instead of passing a data array directly to the insert or update functions */
              $this->db->set($key, $val);               
              }//endif              
           }//endforeach
        }//endforeach

        /* insert the new record into table*/
        $this->db->insert($table); 
        return $this->db->insert_id();
    }

    public function duplicateForeignTableRecord ( $table, $foreign_keys_vals = [],$ignore_key,$update_keys_vals = [] ) 
    {
       /* generate the select query */
       if (is_array($foreign_keys_vals)) {
           foreach ($foreign_keys_vals as $key_field => $key_val) {

               $this->db->where($key_field, $key_val); 
           
           }
       }
       // $this->db->where($foreign_key_field, $foreign_key_val); 

       $query = $this->db->get($table);
      
        foreach ($query->result() as $row){   
           foreach($row as $key => $val){        
              if($key != $ignore_key){ 
              /* $this->db->set can be used instead of passing a data array directly to the insert or update functions */
                $this->db->set($key, $val);               
              }
              foreach ($update_keys_vals as $up_f_key => $up_f_val) {
                  if($key == $up_f_key){
                    $this->db->set($key, $up_f_val); 
                  }
              }
              //endif              
           }//endforeach

           $this->db->insert($table);
        
        }//endforeach

        /* insert the new record into table*/
         
        return $this->db->insert_id();
    }

    // call to function to from controller to model class. 
    public function clone_product($id)
    {
        $product_insert_id = $this->duplicateTableRecord(
            'product',
            ['id_product' => $id],
            'id_product'
        );

        $insert_id = $this->duplicateForeignTableRecord(
            'product_join_category',
            ['id_product' => $id],
            'id_product',
            [ 'id_product' => $product_insert_id ]
        );

        $insert_id = $this->duplicateForeignTableRecord(
            'product_data',
            ['id_product' => $id],
            'id_product_data',
            [ 'id_product' => $product_insert_id ]
        );
        
        $insert_id = $this->duplicateForeignTableRecord(
            'file',
            [ 'id_product' => $id ],
            'id_file',
            [ 'id_product' => $product_insert_id ]
        );
        
        $insert_id = $this->duplicateForeignTableRecord(
            'metatag',
            [ 'section_item_id' => $id, 'section' => 'product' ],
            'id_meta',
            [   'section_item_id' => $product_insert_id ]
        );

        return $product_insert_id;
    }

    // call to the model from controller 
    public function copy($id = '')
    {
        if ($id != '') {

            $record = $this->b_product_model->clone_product($id);
        
        }
        
        // _trace($record);
        redirect(base_url() . 'admin/produkty');
    }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s