CakePHP 3.0中HABTM关联表属性编辑报错求助
Let's walk through the fixes step by step to resolve your "Record not found" error and enable editing the time attribute in your bom_proceso join table:
Step 1: Correct Controller Action Parameter Names & Order
Your main issues here were case sensitivity and incorrect primary key order. Update your BomProcesoController.php actions to match the primary key definition in your table class:
// Corrected view method public function view($bom_id = null, $proceso_id = null) { // Validate both IDs are provided if (!$bom_id || !$proceso_id) { $this->Flash->error(__('Invalid request. Please provide both BOM and Proceso IDs.')); return $this->redirect(['action' => 'index']); } // Fetch record using composite keys in the correct order (matches setPrimaryKey) $bomProceso = $this->BomProceso->get([$bom_id, $proceso_id], [ 'contain' => ['Proceso', 'Bom'] ]); $this->set('bomProceso', $bomProceso); } // Corrected edit method public function edit($bom_id = null, $proceso_id = null) { if (!$bom_id || !$proceso_id) { $this->Flash->error(__('Invalid request. Please provide both BOM and Proceso IDs.')); return $this->redirect(['action' => 'index']); } $bomProceso = $this->BomProceso->get([$bom_id, $proceso_id], [ 'contain' => ['Proceso', 'Bom'] ]); if ($this->request->is(['patch', 'post', 'put'])) { $bomProceso = $this->BomProceso->patchEntity($bomProceso, $this->request->getData()); if ($this->BomProceso->save($bomProceso)) { $this->Flash->success(__('The time attribute has been updated successfully.')); return $this->redirect(['controller' => 'Bom', 'action' => 'view', $bom_id]); } $this->Flash->error(__('Failed to update the time attribute. Please try again.')); } $proceso = $this->BomProceso->Proceso->find('list', ['limit' => 200]); $bom = $this->BomProceso->Bom->find('list', ['limit' => 200]); $this->set(compact('bomProceso', 'proceso', 'bom')); }
Step 2: Add Custom Routes for Composite Keys
Default CakePHP routes don't handle composite primary keys, so add these routes in config/routes.php before the fallback routes:
Router::scope('/', function (RouteBuilder $routes) { // ... existing routes (home, pages) ... // Custom routes for BomProceso composite keys $routes->connect('/bom-proceso/view/:bom_id/:proceso_id', [ 'controller' => 'BomProceso', 'action' => 'view' ], [ 'pass' => ['bom_id', 'proceso_id'], 'bom_id' => '\d+', // Restrict to numeric IDs 'proceso_id' => '\d+' ]); $routes->connect('/bom-proceso/edit/:bom_id/:proceso_id', [ 'controller' => 'BomProceso', 'action' => 'edit' ], [ 'pass' => ['bom_id', 'proceso_id'], 'bom_id' => '\d+', 'proceso_id' => '\d+' ]); // Fallback routes $routes->fallbacks(DashedRoute::class); });
Step 3: Fix Link Generation in Views
When linking to the edit/view actions from your BOM or Proceso views, pass both IDs in the correct order:
// Example in src/Template/Bom/view.ctp foreach ($bom->proceso as $proceso) { echo $this->Html->link(__('Edit Time'), [ 'controller' => 'BomProceso', 'action' => 'edit', $bom->id, // bom_id first (matches primary key order) $proceso->id // proceso_id second ]); }
Key Issues Resolved:
- Case Sensitivity: You were using
$procesoID/$bomIDinstead of the actual parameter names$proceso_id/$bom_id, leading to NULL values in your query. - Primary Key Order: The
get()method expects the composite key array to match the order defined insetPrimaryKey(['bom_id', 'proceso_id'])in your table class. - Route Handling: Custom routes ensure CakePHP correctly maps URLs with two IDs to your controller actions.
These changes will let you properly retrieve and edit the time attribute in your bom_proceso join table.
内容的提问来源于stack exchange,提问作者teph

