cakephp无法将实体保存在MSSQL循环中

问题描述:

使用Microsoft SQL Server 2012(MSSQL),无法循环实体结果并保存它们。之前相同的代码在MySQL中工作正常。cakephp无法将实体保存在MSSQL循环中

第一实体被修改后,服务器将立即抛出这个错误:

错误:SQLSTATE [HY010]:[微软] [ODBC驱动程序11用于SQL Server]函数序列错误

的在实体为save d之后的下一次迭代foreach循环中发生实际错误。简单的代码来演示错误:

$priceRows = $pricesTable 
    ->find() 
    ->where(['event_id' => $eventsQuery->id]); 

foreach($priceRows as $query) { 
    $query->comment = 'new value'; 
    $pricesTable->save($query); 
} 

我试过这两个cakephp 3.1.5和3.3.3与相同的结果。我花了几个小时调试这个,但没有运气。

+2

尝试使用$ priceTable-> get($ eventsQuery-> id)。我认为问题是你正在尝试迭代懒惰的查询对象,而不是实体对象 –

我以前也有类似的问题。

试试这个:

foreach($priceRows as $query) { 
    $pricesTable->patchEntity($query, ['comment' => 'new value'); 
    $pricesTable->save($query); 
} 

我有同样的问题和解决方案已于@马丁Hrabal评论的启发,所以要尽量改变:

$priceRows = $pricesTable 
    ->find() 
    ->where(['event_id' => $eventsQuery->id]); 

$priceRows = $pricesTable 
    ->find() 
    ->where(['event_id' => $eventsQuery->id]) 
    ->toArray();