Codeigniter Datamapper save() not actually saving to database

1.9k views Asked by At

I'm running CodeIgniter 1.7.3, Datamapper DMZ 1.7.1 (and no, upgrading is not an option), MySQL 5.1, and the Cron Job Boostrapper for an application I'm building. I'm attempting to extract information from an XML file and save it into a database. Getting the data works just fine, as does getting data from the database, so I know it's neither an issue with the database connection, nor the XML file.

$this->site = $this->site->get_by_short_name('site');

//Load the XML files into variables so we can do stuff with them.
$doctors = simplexml_load_file(realpath('../xml/doctors.xml'));

foreach ($doctors->children() as $doctor) {
    $dr = new Doctor();
    $attrs = $doctor->attributes();
    /* See if the Doctor already exists. If so, update it.
     * Datamapper won't update fields that don't change, so if nothing's
     * changed, then the DB call won't be made.
     */
    $dr->get_by_drkey($attrs['Key']);
    $dr->drkey = $attrs['Key'];
    $dr->first_name = $doctor->FirstName;
    $dr->middle_name = $doctor->MiddleName;
    $dr->last_name = $doctor->LastName;
    $dr->long_name = $doctor->LongName;
    $dr->degrees = $doctor->Degrees;
    $dr->pic = $doctor->ImageURL;
    $dr->save($this->site);
}

I've checked the return value of $dr->save($this->site) and it's coming back as true ("successful save"), but the data isn't saving, and there's no error. I've tried without the relation (removed the requirement for it in the database), so it should save without error, but it still doesn't save. I also double-checked the relationship setup in the models and they're fine.

I also tried bypassing Datamapper and doing a straight $this->db->insert('doctors',$data) with the data converted to an array, but the data still doesn't save. I've also tried running it from the browser (bypassing the bootstrapper) to see if the issue had to do with running from the command line, but that also didn't work. I'm also not autoloading any session or authentication libraries, so that known issue with the bootstrapper isn't being triggered.

Does anyone have any insight into why this might not be working?

1

There are 1 answers

0
Shauna On BEST ANSWER

I've found what was going on. It turns out that the attributes of the SimpleXML objects (the individual records) weren't getting picked up by Datamapper as strings, so they weren't getting escaped, and MySQL was choking. Typecasting them when setting the model object's values ($dr->first_name = (string)$doctor->First_Name;) fixed this problem.

I was able to get the insert query through $dr->check_last_query() after the object attempts to save.