简体   繁体   中英

Can't edit an attribute in a HABTM association with CakePHP

this is a fragment of my database. Model

I used CakePHP 3.0 Bake command to create controllers, models and views. As you see I got a HABTM association but also it has an attribute. I can insert data using the BOM view and the proceso view. But, when I try to edit the time associated with these things I got the message

Record not found in table "bom_proceso" with primary key ['1']

I need to edit that attribute and I don't know how to do with that error or if I made something wrong.

BomProcesoTable Model

class BomProcesoTable extends Table
/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);

    $this->setTable('bom_proceso');
    $this->setDisplayField('bom_id');
    $this->setPrimaryKey(['bom_id', 'proceso_id']);

    $this->belongsTo('Proceso', [
        'foreignKey' => 'proceso_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Bom', [
        'foreignKey' => 'bom_id',
        'joinType' => 'INNER'
    ]);
}

/**
 * Default validation rules.
 *
 * @param \Cake\Validation\Validator $validator Validator instance.
 * @return \Cake\Validation\Validator
 */
public function validationDefault(Validator $validator)
{
    $validator
        ->numeric('time')
        ->allowEmpty('time');

    return $validator;
}

/**
 * Returns a rules checker object that will be used for validating
 * application integrity.
 *
 * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
 * @return \Cake\ORM\RulesChecker
 */
public function buildRules(RulesChecker $rules)
{
    $rules->add($rules->existsIn(['proceso_id'], 'Proceso'));
    $rules->add($rules->existsIn(['bom_id'], 'Bom'));

    return $rules;
}

Controller view function

public function view($id = null)
{
    $bomProceso = $this->BomProceso->get($id, [
        'contain' => ['Proceso', 'Bom']
    ]);

    $this->set('bomProceso', $bomProceso);
}

UPDATE

Thanks to @chTag, I edited the controller, now is like this

public function view($proceso_id = null, $bom_id=null)
{
    $bomProceso = $this->BomProceso->get([$procesoID, $bomID], [
    'contain' => ['Proceso', 'Bom']
      ]);


    $this->set('bomProceso', $bomProceso);
}

But, the query condition I can see now is WHERE(BomProceso.bom_id = 1 AND BomProceso.proceso_id = NULL)

The file routes.php that I can find at config folder is now as you see below

config/routes.php

use Cake\Core\Plugin;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\Routing\Route\DashedRoute;

/**
 * The default class to use for all routes
 *
 * The following route classes are supplied with CakePHP and are appropriate
 * to set as the default:
 *
 * - Route
 * - InflectedRoute
 * - DashedRoute
 *
 * If no call is made to `Router::defaultRouteClass()`, the class used is
 * `Route` (`Cake\Routing\Route\Route`)
 *
 * Note that `Route` does not do any inflections on URLs which will result in
 * inconsistently cased URLs when used with `:plugin`, `:controller` and
 * `:action` markers.
 *
 */
Router::defaultRouteClass(DashedRoute::class);

Router::scope('/', function (RouteBuilder $routes) {
    /**
     * Here, we are connecting '/' (base path) to a controller called 'Pages',
     * its action called 'display', and we pass a param to select the view file
     * to use (in this case, src/Template/Pages/home.ctp)...
     */
    $routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);

    /**
     * ...and connect the rest of 'Pages' controller's URLs.
     */
    $routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);

    /**
     * Connect catchall routes for all controllers.
     *
     * Using the argument `DashedRoute`, the `fallbacks` method is a shortcut for
     *    `$routes->connect('/:controller', ['action' => 'index'], ['routeClass' => 'DashedRoute']);`
     *    `$routes->connect('/:controller/:action/*', [], ['routeClass' => 'DashedRoute']);`
     *
     * Any route class can be used with this method, such as:
     * - DashedRoute
     * - InflectedRoute
     * - Route
     * - Or your own route class
     *
     * You can remove these routes once you've connected the
     * routes you want in your application.
     */
    $routes->fallbacks(DashedRoute::class);
});

/**
 * Load all plugin routes. See the Plugin documentation on
 * how to customize the loading of plugin routes.
 */
Plugin::routes();

The controller source code that was generated is wrong. It was generated to use a single primary key, but should have been a compound key.

public function view($bom_id = null, $proceso_id = null)
{
    $bomProceso = $this->BomProceso->get([$bom_id, $proceso_id], [
        'contain' => ['Proceso', 'Bom']
    ]);

    $this->set('bomProceso', $bomProceso);
}

You'll have to check your routes.php to see if the two parameters will be passed. I'm going to guess that it's configured to just pass :id with a default routing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM