조인 테이블의 기존 데이터를 업데이트하는 데 문제가 있습니다. 여기에 ...CakePHP 3 조인 테이블의 데이터 업데이트
두 모델이 SalesOrders이고 LineItems를 통해 belongsToMany 연관이있는 제품이 있습니다.
products
+-------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| name | int(11) | NO | | NULL | |
+-------------+---------------------+------+-----+---------+----------------+
sales_orders
+--------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| order_number | int(11) | NO | | NULL | |
+--------------+---------------------+------+-----+---------+----------------+
line_items
+----------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| sales_order_id | int(11) unsigned | NO | | NULL | |
| product_id | int(11) unsigned | NO | | NULL | |
| qty | int(11) | NO | | NULL | |
+----------------+---------------------+------+-----+---------+----------------+
새로운 판매 주문 작성은 예상대로 작동합니다. 새로운 판매 주문 추가 페이지에서 판매 주문에 배수 상품 및 수량을 추가 할 수 있습니다. 모든 올바른 데이터가 조인 테이블에 추가됩니다.
내가 가지고있는 문제는 기존 판매 주문을 편집하려고 할 때입니다. 예를 들어 특정 광고 항목의 수량을 5에서 2로 변경하고 싶습니다. 수정할 판매 주문의 편집 페이지를 열고 원하는 광고 항목의 수량을 변경 한 다음 양식을 제출합니다. 양식은 성공적으로 제출되지만 조인 테이블은 갱신되지 않습니다. 이것은 작동하지 않을 수있는 아주 기본적인 기능처럼 보입니다. 모든 코드는 몇 가지 작은 수정으로 구워졌습니다.
SalesOrdersTable :
$this->belongsToMany('Products', [
'foreignKey' => 'sales_order_id',
'targetForeignKey' => 'product_id',
'through' => 'LineItems',
]);
ProductsTable :
$this->belongsToMany('SalesOrders', [
'foreignKey' => 'product_id',
'targetForeignKey' => 'sales_order_id',
'through' => 'LineItems',
]);
LineItemsTable :
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'INNER'
]);
$this->belongsTo('SalesOrders', [
'foreignKey' => 'sales_order_id',
'joinType' => 'INNER'
]);
SalesOrdersController 편집 :
public function edit($id = null)
{
$salesOrder = $this->SalesOrders->get($id, [
'contain' => ['Products']
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$salesOrder = $this->SalesOrders->patchEntity($salesOrder, $this->request->data);
if ($this->SalesOrders->save($salesOrder)) {
$this->Flash->success(__('The sales order has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The sales order could not be saved. Please, try again.'));
}
}
$products = $this->SalesOrders->Products->find('list', ['limit' => 200]);
$this->set(compact('salesOrder', 'products'));
$this->set('_serialize', ['salesOrder']);
}
,691 363,210
SalesOrders 템플릿 edit.ctp 당신의 더러운 속성을 볼 수 있듯이 2
object(App\Model\Entity\SalesOrder) {
'id' => (int) 1,
'order_number' => 'SO1111',
'products' => [
(int) 0 => object(App\Model\Entity\Product) {
'id' => '1',
'name' => 'Acme Widget 1',
'_joinData' => object(App\Model\Entity\LineItem) {
'id' => (int) 1,
'product_id' => (int) 1,
'sales_order_id' => (int) 1,
'qty' => (int) 2,
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'qty' => true
],
'[original]' => [
'qty' => (int) 5
],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'LineItems'
},
'[new]' => false,
'[accessible]' => [
'*' => true,
'_joinData' => true
],
'[dirty]' => [
'_joinData' => true
],
'[original]' => [
'_joinData' => object(App\Model\Entity\LineItem) {
'id' => (int) 1,
'product_id' => (int) 1,
'sales_order_id' => (int) 1,
'qty' => (int) 2,
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'qty' => true
],
'[original]' => [
'qty' => (int) 5
],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'LineItems'
}
],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Products'
}
],
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'SalesOrders'
}
5에서 수량을 변경 양식을 제출하면 SalesOrder를 엔티티는 다음과 같습니다
echo $this->Form->input('order_number');
echo $this->Form->input('products.0.id', [
'type' => 'select',
'options' => $products
]);
echo $this->Form->input('products.0._joinData.qty');
SalesOrder가 비어 있습니다. _joinData 제품이 수정되었음을 알 수 없습니다.
$ salesOrders-> dirty ('products', true)를 추가하면; save()가 호출되기 바로 전에 조인 테이블이 업데이트됩니다. 이 방법이 효과가 있고, 이런 식으로 업데이트를 처리 할 수있는 논리를 쓸 수는 있지만, 더 나은/올바른 방법이 있다고 느낍니다.
도움을 주시면 감사하겠습니다.