2011-02-25 136 views
1

我爲應用程序運行CodeIgniter 1.7.3,Datamapper DMZ 1.7.1(和不,升級不是選項),MySQL 5.1和Cron Job Boostrapper建造。我試圖從XML文件中提取信息並將其保存到數據庫中。從數據庫獲取數據,獲取數據工作得很好,因此我知道這不是數據庫連接問題,也不是XML文件。Codeigniter Datamapper保存()實際上並沒有保存到數據庫

$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); 
} 

我檢查的$dr->save($this->site)的返回值,它會回來爲真(「保存成功」),但數據不保存,而且也沒有錯誤。我試過沒有關係(在數據庫中刪除了它的要求),所以它應該保存沒有錯誤,但它仍然不會保存。我還仔細檢查了模型中的關係設置,他們很好。

我也嘗試繞過Datamapper並做直接$this->db->insert('doctors',$data)與數據轉換爲數組,但數據仍然不保存。我也嘗試從瀏覽器運行它(繞過引導程序),看看問題是否與從命令行運行有關,但這也不起作用。我也沒有自動加載任何會話或認證庫,因此引導程序的已知問題未被觸發。

有沒有人有任何洞察,爲什麼這可能不工作?

回答

0

我發現發生了什麼事。事實證明,SimpleXML對象(單個記錄)的屬性沒有被Datamapper作爲字符串拾取,所以它們沒有被轉義,並且MySQL正在窒息。在設置模型對象的值($dr->first_name = (string)$doctor->First_Name;)時輸入它們可以解決此問題。

在對象嘗試保存後,我能夠通過$dr->check_last_query()獲得插入查詢。