无法保存Doctrine_Collection
问题描述:
我使用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();
}
}
答
好的我发现了这个问题。我使用MYSQL保留字顺序作为字段名称,从而将错误更改为sortOrder并且问题消失。
希望这可以帮助有类似问题的人。
Garry