无法编辑habtm与cakephp关联中的属性

eqfvzcg8  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(326)

这是我数据库的一个片段。模型
我使用cakephp3.0bake命令来创建控制器、模型和视图。如你所见,我有一个habtm关联,但它也有一个属性。我可以使用bom视图和proceso视图插入数据。但是,当我试图编辑与这些事情相关的时间时,我得到了信息
在主键为['1']的表“bom\u proceso”中找不到记录
我需要编辑该属性,我不知道如何处理该错误,或者如果我做错了什么。
可预测模型

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

控制器视图功能

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

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

更新
感谢@chtag,我编辑了控制器,现在是这样的

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

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

但是,我现在看到的查询条件是 WHERE(BomProceso.bom_id = 1 AND BomProceso.proceso_id = NULL) 文件 routes.php 我可以在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();
nmpmafwu

nmpmafwu1#

生成的控制器源代码错误。它是使用单个主键生成的,但应该是复合键。

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

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

你得检查一下你的行李 routes.php 看看这两个参数是否会被传递。我猜它的配置就是通过 :id 使用默认路由。

相关问题