Thursday, January 15, 2015

CVS to SQL (Part 2 of yesterday)

This is pretty awkward

As it turns out, my task yesterday wasn't complete.

The reason is this. The data I pulled has 2 kinds of delimiter, 1 for column_name(in this case, comma) and a second delimiter for values(in my case, bar-|)

So at the end what I imported to my db is

id        code        name
1         AB          value1|value2|value3
2         CD          value4|value5|value6

Which is obviously not what I want.
Just in case I myself forget what I am looking for in the future, I am hoping that the name column gets broken down, and get the following

id        code        name
1         AB          value1
2         AB          value2
3         AB          value3
4         CD          value4

Hence, what I ended up doing is break this table to 2 separate tables manually. A code table and a name table. P/S: Similar to countries vs states

So the approach here is to create a dummy route, link to that function, and create the function just for the sake of writing this to the database (make sure to remove this route and function afterwards.

Due to NDA, I decided to name the variables foo for table1, bar for table2 and foo_bar for the combined table that I imported initially.

    public function getFoo(){

$jsonResponse = new JsonResponse();
$response = '';

$barName = Bar::getFooByBar();



for($i = 0; $i < count($barName); $i++){

$fooArray = explode('|', $barName[$i]->foo);




foreach ($FooArray as $FooName) {

if($FooName != ''){
$foo_database = new Foo();
DB::table('foo')->insert(
array('bar_id' => $i+1, 'name' => $fooName)
);
}
}
}

$jsonResponse->setResponse($fooArray);

return $jsonResponse
->get();

}

The idea here is to get everything($barName), loop it and tokenize the index with Bar-| and write it to the database.
Don't mind the setResponse($fooArray), I simply use that for debugging process, not wise, but just for convenience since this function is to be removed immediately after I wrote everything to database.

Oh oh! And I just experimented something neat today. After importing foo_bar, since it is only foo that has everything concatenated, I ended up cloning foo_bar,(let's call it foo_bar2 here) then drop foo from foo_bar2, and add a primary key(id), set it to auto increment, it will intelligently assign the ID to existing rows in foo_bar2. Which is neat cause this means that I immediately have 1 table done :)

No comments:

Post a Comment