Adding records in Batch
Hi,
I have about 2000 records of persons and about 800 counties and a whole number of facilities in a excel file. Is there a way to add these records in a batch way style in one shot? Like export my data to a CSV and important it into the database. I noticed there are a number of tables updated when you add for example a county record and this makes it tricky to just target one table for adding the bulk data. Anybody with the solution on this?
Regards,
Chanda
Question information
- Language:
- English Edit question
- Status:
- Solved
- Assignee:
- No assignee Edit question
- Solved by:
- ehr
- Solved:
- Last query:
- Last reply:
Revision history for this message
|
#1 |
Yes, it is a bit of a pain to try to add the data directly to the mysql tables -- this is so the system can track the changes to the data and so that you can dynamically add fields and forms to the system.
We have had to do this a number of times, and I would suggest making use of the forms (look at i2ce/modules/
The geographic data is a bit complex, but has to be done before you can add people... you will have to add first all the countries (you may have only one). Then any regions, then the districts, then the counties for that district. You have to make sure that as you go along, you set the appropriate links between the data. For example you could do something like (I haven't tested this script, but it should be a fair enough outline):
$factory = I2CE_FormFactor
$user = new I2CE_User();
foreach ($geographic_data as $country=
if (! $country_
}
//now we need to add in the regions for each country.
foreach ($country_
if (!$region_
}
* Insert code to handle the districts, and then the counties under the districts
* foreach ($region_
}
}
Note: the cleanup function is so the memory used by creating the form will be return to the system by
the garbage collector.
Now we can add in the people
Suppose you have a 'people.csv' file with each row corresponding to a person. Use whatever you want to read in the
CSV file into the array $people whose values are the row the the CSV file, already split up into the columns.
Suppose the columns are laid out like so:
0 = firstname
1 = middle name
2 = lastname
3 = nationality
4 = country of residence
6 = region of residence
5 = district of residence
Then you would do (in the same script):
foreach ($people as $i=>$cols) {
if (!$person_
}
}
If you need so see what fields are available for each form and which field is required for each form, the easiest place is to look in the form browser. For example, look under
to see what class the person form is associated to (iHRIS_
/
to see the available fields and the information about them. You will see under
/
that iHRIS_ManagePerson extends iHRIS_Person, so you can check for more fields under
/
Revision history for this message
|
#2 |
I forgot to mention, that the magic data browser, once it has been enabled under "Configure System->Configure Modules", can be accessed under "Configure System->Browse Magic Data"
Revision history for this message
|
#3 |
Hi Carl,
Thanks for the answer. Yes I get the idea , thanks. However, am just lost on
where and how to call & install the script. Do I run it on the command line
like
php -a
// my scripts here
exit ();
or do I create a form that I can call through the browser and the upload my
csv file?
Sorry for asking a lot of newbie questions.
Chanda
On Thu, Nov 6, 2008 at 7:38 PM, Carl Leitner <
<email address hidden>> wrote:
> Your question #50419 on iHRIS Manage changed:
> https:/
>
> Carl Leitner proposed the following answer:
> I forgot to mention, that the magic data browser, once it has been
> enabled under "Configure System->Configure Modules", can be accessed
> under "Configure System->Browse Magic Data"
>
> --
> If this answers your question, please go to the following page to let us
> know that it is solved:
>
> https:/
>
> If you still need help, you can reply to this email or go to the
> following page to enter your feedback:
> https:/
>
> You received this question notification because you are a direct
> subscriber of the question.
>
Revision history for this message
|
#4 |
I would run it from the command line. Are you starting with the "blank" site?
If so, change to the 'manage/
index.php script. Copy it over to something like 'importer.php'. Delete
the lines:
$page = new Wrangler();
$page-
And put in your import script there. Then, once the script is written, you can
do simply:
php importer.php
(If you want to do the script piece by piece rather than in one go, you
can do lookups of data that was already entered in. We can talk about
that if you need to)
I'm sorry for the lack of documentation.
cheers,
-carl
Revision history for this message
|
#5 |
I added some documentation for forms, formClasses and how they are related at:
http://
You may also want to check out:
http://
though it may be a bit out of date.
Revision history for this message
|
#6 |
Hi Carl,
Yes am starting with a bank site. Am actually trying to migrate data from
TIMS by JHPIEGO to IHRIS Manage. I have managed to get all the data in a
excel format and am still struggling to import the data into Manage. Please
find attached the two files with sample records am trying to migrate. I have
like 800 facilities and over 2000 persons and I just want to load this base
data in one short
Regards
Boniface
On Fri, Nov 7, 2008 at 2:17 PM, Carl Leitner <
<email address hidden>> wrote:
> Your question #50419 on iHRIS Manage changed:
> https:/
>
> Status: Open => Answered
>
> Carl Leitner proposed the following answer:
> I would run it from the command line. Are you starting with the "blank"
> site?
> If so, change to the 'manage/
> index.php script. Copy it over to something like 'importer.php'. Delete
> the lines:
> $page = new Wrangler();
> $page->wrangle();
> And put in your import script there. Then, once the script is written, you
> can
> do simply:
> php importer.php
>
> (If you want to do the script piece by piece rather than in one go, you
> can do lookups of data that was already entered in. We can talk about
> that if you need to)
>
> I'm sorry for the lack of documentation.
> cheers,
> -carl
>
> --
> If this answers your question, please go to the following page to let us
> know that it is solved:
>
> https:/
>
> If you still need help, you can reply to this email or go to the
> following page to enter your feedback:
> https:/
>
> You received this question notification because you are a direct
> subscriber of the question.
>
Revision history for this message
|
#7 |
For some reason your attachment didn't go through. Can you email me it directly?
<email address hidden>
Cheers,
-carl
Revision history for this message
|
#8 |
Try this script. It worked for me on your data files (exported to CSV) working
off the blank site:
<?php
/*
* © Copyright 2007, 2008 IntraHealth International, Inc.
*
* This File is part of iHRIS
*
* iHRIS is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://
*/
/**
* @author Carl Leitner <email address hidden>
* @copyright Copyright © 2007, 2008 IntraHealth International, Inc.
*/
$people_file = 'persons.csv';
$facility_file = 'facilities.csv';
if (!is_readable(
die("Can't read $people_file\n");
}
if (!is_readable(
die("Can't read $facility_file\n");
}
require_once( dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config.
$local_config = dirname(__FILE__) . DIRECTORY_SEPARATOR .
'local' . DIRECTORY_SEPARATOR . 'config.
if (file_exists(
require_
}
if(!isset(
echo "Please set the \$i2ce_
exit(55);
}
require_once ($i2ce_
I2CE::initializ
);
unset($
unset($
unset($
unset($
unset($
unset($
$factory = I2CE_FormFactor
$user = new I2CE_User();
$lists = array();
function geo_data(
if (!is_string(
return 0;
}
$country_id = getListId(
if (!is_string(
return $country_id;
}
$region_id = getListId(
if (!is_string(
return $region_id;
}
$district_id = getListId(
if (!is_string(
return $district_id;
}
return getListId(
}
function getListId( $list_name, $value, $top_link = '', $top_link_id = 0, $add_fields = array()) {
global $lists;
global $user;
global $factory;
if (!array_
}
$list =&$lists[
if (!array_
}
if (!array_
//get any existing list information in the database.
}
$values =&$list[
$id = array_search(
if ($id !== false) {
return $id;
}
//we did not find the value in the database. we need to add it.
$form = $factory-
$form->name = $value;
foreach ($add_fields as $field=>$value) {
}
if ($top_link_id > 0) {
}
if (! $form->save($user)) {
echo "Could not save the $list_name $value\n";
return false;
}
$id = $form->getId();
$values[$id] = $value;
$form-
return $id;
}
// We are supposing you have a 'facility.csv' file with each row corresponding to a facility
// Suppose the columns are laid out like so:
//"Country"
//"South Africa","Eastern Cape","Alfred Nzo","Amahlathi
$fh = fopen($
if (!is_resource($fh)) {
die("Bad facilities file $facility_file\n");
}
//skip the first line
fgetcsv($fh);
$i =1;
$facility_types = array();
$skip_facility = null;
while ( ($cols = fgetcsv($fh)) !== false) {
$i++;
if (count($cols) != 10) {
echo "Bad data on line $i:" . implode(',',$cols) . "\n\tHave " . count($cols) . " instead of 10\n";
continue;
}
list(
$country_id = geo_data($country);
$region_id = geo_data(
$district_id = geo_data(
$county_id = geo_data(
$facility_
$facility_id = getListId(
if ($facility_id == 0) {
echo "Bummer\n";
continue;
}
$facilityForm = $factory-
if (!$facilityForm instanceof I2CE_Form) {
die("Cannot get facility form with id $facility_id\n");
}
$existing_
if (count(
continue;
}
$contactForm = $factory-
$contactFor
$contactFor
$contactFor
$contactFor
$contactFor
$contactFor
if (!$contactForm-
echo "Could not save the contact at row $i:\n\t" . implode (',', $cols) . "\n";
}
$contactFor
$facilityFo
}
// We are supposing you have a 'people.csv' file with each row corresponding to a person.
// Suppose the columns are laid out like so:
//"FIRST NAME","
//"Bonga"
$fh = fopen($
if (!is_resource($fh)) {
die("Bad people file $people_file\n");
}
//skip the first line
fgetcsv($fh);
$i =1;
$skip_person = null;
while ( ($cols = fgetcsv($fh)) !== false) {
$i++;
if (count($cols) != 7) {
echo "Bad data on line $i:" . implode(',',$cols) . "\n\thave " . count($cols) . " instead of 7\n";;
continue;
}
list(
$country_id = geo_data($country);
$district_id = geo_data(
$county_id = geo_data(
$existing= iHRIS_Person:
array(
)
));
if (count($existing) > 0) {
if (prompt("The person $firstname $surname already exists. Should I skip creation of this person?", $skip_person)) {
echo "Skipping line " . ($i -1 ) . ": " . implode(",",$cols) . "\n";
}
}
$person_form = $factory-
$person_
$person_
$person_
$person_
$person_
$person_
$person_
if (!$person_
echo "Could not save the person at row $i:\n\t" . implode (',', $cols) . "\n";
}
$person_
}
function prompt ($message,
if ($universal === true) {
return true;
}
if ($universal === false) {
return false;
}
echo "$message: (Yes/No/
$success =false;
while (true) {
$c = strtolower(
if ($c=== false) {
echo ("Bad input");
die();
}
switch ($c) {
case 'y':
return true;
case 'n':
return false;
case 'a':
return true;
case 'v':
return false;
}
}
echo "\n";
}
# Local Variables:
# mode: php
# c-default-style: "bsd"
# indent-tabs-mode: nil
# c-basic-offset: 4
# End:
Revision history for this message
|
#9 |
Hi Carl,
Thanks for the script. It worked well despite a few insignificant issues here and there. I also got this error, but it did not affect the import. What could be the issue here
php importer.php
I2CE: I2CE_MagicData-
Thanks again for taking time off your busy schedule to help me with this.
Chanda
Revision history for this message
|
#10 |
The "magic data" is stored in the database. Because it is accessed quite heavily on the
webserver, we use APC:
http://
to cache the results of the database into shared memory which is then shared across
requests. This message is just notifying you that you the APC cache is not available
from the command line. As we are not doing any kind of repeated access to the
database here, you can safely ignore this message. However, if you are seeing it in the
apache log, that means the system is going to run slow, and you'll need to enable the
apc module.
BTW... to monitor for error messages in the apache log file, there is a utility that you
can use to make the messages more legible:
I2CE/
-carl
Revision history for this message
|
#11 |
Hi Carl,
What is the deal with INT values? When adding simple fields to the form in the xml with code like below, the INT value for the <displayName>The form field type</displayName> does not show up in the form, but STRING_LINE and DATE_YMD works fine. Is there any special treatment the INT needs?
Chanda
Revision history for this message
|
#12 |
Hi Carl,
It now works after adding this to the configuration
----
--
Chanda