• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with inserting csv file entries to db

#1
What i'm facing here has kinda confused me.
I'm trying to insert some entries parsed from csv file into db. I found the csv library, it works and so far so good.

I have the following script in controller: "Products/insert",

Code:
if ($this->csvimport->get_array($file_path)) {
$csv_array = $this->csvimport->get_array($file_path);
  foreach ($csv_array as $row) {
   $insert_data = [];
   $insert_data['product_id']    = $row['id'];
   $insert_data['model'    ]    = $row['ean'];
   $insert_data['stock']    = $row['stock'];
   $insert_data['price']    = $row['price_w_o_vat'];
   $insert_data['noise']    = $row['noise'];
   $insert_data['warehouse']    = $row['warehouse'];
   $insert_data['minimum']    = 1;
   $insert_data['supplier']    = 1;
   $insert_data['image']    = $row['photo'];
   $insert_data['created_at']    = date('Y-m-d H:i:s');

   $manuf_exists = $this->product_model->manufacturer_exists($row['manufacturer']);
   if($manuf_exists !== FALSE){
    $insert_data['manufacturer_id'] = $manuf_exists;
   } else {
    $manufacturer_data['manufacturer_name'] = $row['manufacturer'];
    $manufacturer_data['manufacturer_slug'] = strtolower($row['manufacturer'].'-tyres');
    $add_manuf = $this->product_model->add_manufacturer($manufacturer_data);
    $insert_data['manufacturer_id'] = $add_manuf['manufacturer_id'];
   }
    $this->product_model->insert_product($insert_data);
}
                
}

And here's my model:
Code:
    public function insert_product($data){
        $query = $this->db->insert('products', $data);
        if($query){
            return true;
        } else {
            return false;
        }
    }

    public function manufacturer_exists($manufacturer){
        $this->db->select('manufacturer_id');
        $this->db->where('manufacturer_name', $manufacturer);
        $query = $this->db->get('manufacturers');
        if($query->num_rows() > 0){
            return $query->row()->manufacturer_id;
        } else {
            return false;
        }
    }

    public function add_manufacturer($data){
        $insert = $this->db->insert('manufacturers', $data);
        if($insert){
            return $this->db->insert_id();
        } else {
            return false;
        }
    }

I've also tried batch_insert but i can't check other tables and return the id. If anyone has ever made it, please feel free to suggest a solution on this..

Thanks in advance...

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply

#2
@HarrysR,

What are you trying to accomplish? Inserting records? Get the id of the created records? Are you getting error messages? If so, what error messages are you receiving?
Reply

#3
(05-16-2019, 09:40 PM)php_rocs Wrote: @HarrysR,

What are you trying to accomplish? Inserting records? Get the id of the created records?  Are you getting error messages? If so, what error messages are you receiving?

Yeap that's some very good questions i forgot to answer.
So...
I have two tables (for now) [products & manufacturers].
What i want to achieve is to upload a csv file and check if the manufacturer exists. If so, then i want to parse the id, and pass it to the "products" table. If not then create the manufacturer entry and get its ID.

What i only get is that the column "manufacturer_id" (which is the one i want to get) can not be NULL.. And i don't know why i get this error.

As far as i can see the query is right....

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply

#4
@HarrysR,

What does your csv input look like (test data)? Also, what tools do you use to debug your code? Xdebug? echo statements? My guest is that there is a problem with your manufacturer-exist method.
Reply

#5
(05-17-2019, 06:53 AM)php_rocs Wrote: @HarrysR,

What does your csv input look like (test data)?  Also, what tools do you use to debug your code? Xdebug? echo statements?  My guest is that there is a problem with your manufacturer-exist method.

Well i feel insanely stupid rn since the solution was really simple and silly.
To begin with, to answer your question, i use "echo" statements.

On the other hand as for the solution here's what was wrong.

In my controller i had this:
Code:
$add_manuf = $this->product_model->add_manufacturer($manufacturer_data);
$insert_data['manufacturer_id'] = $add_manuf['manufacturer_id'];

While the model was like that:
Code:
public function add_manufacturer($data){
$insert = $this->db->insert('manufacturers', $data);
if($insert){
  return $this->db->insert_id();
} else {
  return false;
}
}

So (for people like me who didn't see that coming), while the function returned the last inserted_id of the query, i was trying to access the value as if the sql query returned a row_array.

In two words.... i had to do it like this:
Code:
[BEFORE]
$add_manuf = $this->product_model->add_manufacturer($manufacturer_data);
$insert_data['manufacturer_id'] = $add_manuf['manufacturer_id'];

[AFTER]
$add_manuf = $this->product_model->add_manufacturer($manufacturer_data);
$insert_data['manufacturer_id'] = $add_manuf;


Thank you for your interest in finding the problem guys!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.