0
У меня есть модель товара с Категория:Изменение значения поля из нескольких строк с saveMany()
class Item extends AppModel {
…
public $hasAndBelongsToMany = array(
'Category' =>
array(
'className' => 'Category',
'joinTable' => 'categories_items',
'foreignKey' => 'item_id',
'associationForeignKey' => 'category_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
…
}
У меня есть то же самое для другой стороны, категория. Это означает, что у меня есть таблица соединений с именем CategoriesItems.
Я хочу обновить поле таблицы Item для всех строк.
$dataSource = $this->Item->getDataSource();
$dataSource->begin();
$items = $this->Item->find('all');
$i = 1;
$data = array();
foreach($items as $item) {
if($i == $newPosition) {
$movingItem['Item']['position'] = $newPosition;
$data[] = $movingItem;
$i++;
}
$item['Item']['position'] = $i;
$data[] = $item;
$i++;
}
if($this->Item->saveMany($data, array('deep' => true))) {
$dataSource->commit();
$this->autoRender = false;
header("HTTP/1.0 200 OK");
return true;
}
else {
$dataSource->rollback();
$this->autoRender = false;
header("HTTP/1.0 404 Not Found");
return false;
}
Похоже, что мой массив данных хорош. Но когда я пытаюсь его сохранить, строки CategoriesItems удаляются.