You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CakePHP 3.0中HABTM关联表属性编辑报错求助

Fixing Composite Primary Key Handling for HABTM Join Table in CakePHP 3.0

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);
});

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:

  1. Case Sensitivity: You were using $procesoID/$bomID instead of the actual parameter names $proceso_id/$bom_id, leading to NULL values in your query.
  2. Primary Key Order: The get() method expects the composite key array to match the order defined in setPrimaryKey(['bom_id', 'proceso_id']) in your table class.
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:06:52