2012-08-07 55 views
0

我使用Zend Framework的Docrine 1.2並試圖保存一個Doctrine Collection。無法保存Doctrine_Collection

我使用下面的代碼從我的表類中檢索我的集合。

public function getAll() 
{ 
    return $this->createQuery('e') 
    ->orderBy('e.order ASC, e.eventType ASC') 
    ->execute(); 
} 

我也有以下類來重新排序上述事件記錄。

class Admin_Model_Event_Sort extends Model_Abstract 
{ 
    /** 
    * Events collection 
    * @var Doctrine_Collection 
    */ 
    protected $_collection = null; 

    public function __construct() 
    { 
     $this->_collection = Model_Doctrine_EventTypesTable::getInstance()->getAll(); 
    } 

    public function save($eventIds) 
    { 
     if ($this->_collection instanceof Doctrine_Collection) { 
      foreach ($this->_collection as $record) 
      { 
       $key = array_search($record->eventTypeId, $eventIds); 
       if ($key !== false) { 
        $record->order = (string)$key; 
       } 
      } 
      return $this->_saveCollection($this->_collection); 
     } else { 
      return false; 
     } 
    } 

} 

上述_saveCollection方法如下

/** 
* Attempts to save a Doctrine Collection 
* Sets the error message property on error 
* @param Doctrine_Collection $collection 
* @return boolean 
*/ 
protected function _saveCollection(Doctrine_Collection $collection) 
{ 
    try { 
     $collection->save(); 
     return true; 
    } catch (Exception $e) { 
     $this->_errorMessage = $e->getMessage(); 
     OpenMeetings_Logger_ErrorLogger::write('Unable to save Doctrine Collection'); 
     OpenMeetings_Logger_ErrorLogger::vardump($this->_errorMessage); 
     return false; 
    } 
} 

事件ID的上述保存方法是簡單地事件的ID的枚舉數組,我使用的陣列的鍵設置的排序使用訂單字段的事件順序。如果我將一個var_dump集合傳遞給一個數組($ this - > _ collection-> toArray()),我會得到正確的數據。但是,當我嘗試保存集合時,出現以下錯誤。

「SQLSTATE [42000]:語法錯誤或訪問衝突:1064您的SQL語法有錯誤;請查看與您的MySQL服務器版本相對應的手冊,以找到在'order ='0'附近使用的正確語法WHERE eventtypeid ='3''在第1行「

是否有無論如何我可以得到教義擴大這個錯誤,完整的SQL語句將是一個開始,如果有人知道爲什麼這個錯誤發生那麼會非常有幫助。

提前感謝

加里

編輯

我已經修改了我上面的代碼,試圖在上班時間一個記錄,但我仍然得到了同樣的問題。

public function save($eventIds) 
    { 
     foreach ($eventIds as $key => $eventId) { 
      $event = Model_Doctrine_EventTypesTable::getInstance()->getOne($eventId); 
      $event->order = (string)$key; 
      $event->save(); 
     } 

    } 

回答

1

好的我發現了這個問題。我使用MYSQL保留字順序作爲字段名稱,從而將錯誤更改爲sortOrder並且問題消失。

希望這可以幫助有類似問題的人。

Garry