简体   繁体   English

Codeigniter Datamapper save()并未实际保存到数据库

[英]Codeigniter Datamapper save() not actually saving to database

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. 我正在为要构建的应用程序运行CodeIgniter 1.7.3,Datamapper DMZ 1.7.1(并且没有,不能进行升级),MySQL 5.1和Cron Job Boostrapper I'm attempting to extract information from an XML file and save it into a database. 我正在尝试从XML文件提取信息并将其保存到数据库中。 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. 从数据库获取数据就像从数据库获取数据一样正常,因此我知道这既不是数据库连接问题也不是XML文件问题。

$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. 我检查了$dr->save($this->site)的返回值,它返回为true(“成功保存”),但是数据没有保存,也没有错误。 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. 我还尝试绕过Datamapper,并直接将$this->db->insert('doctors',$data)与转换为数组的数据进行比较,但数据仍然无法保存。 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? 是否有人对为什么这可能行不通有任何见解?

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. 事实证明,Datamapper不会将SimpleXML对象的属性(各个记录)作为字符串接收,因此不会被转义,而MySQL则令人窒息。 Typecasting them when setting the model object's values ( $dr->first_name = (string)$doctor->First_Name; ) fixed this problem. 在设置模型对象的值时进行类型转换( $dr->first_name = (string)$doctor->First_Name; )解决了此问题。

I was able to get the insert query through $dr->check_last_query() after the object attempts to save. 对象尝试保存后,可以通过$dr->check_last_query()获得插入查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM