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

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

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

  1. class BomProcesoTable extends Table
  2. /**
  3. * Initialize method
  4. *
  5. * @param array $config The configuration for the Table.
  6. * @return void
  7. */
  8. public function initialize(array $config)
  9. {
  10. parent::initialize($config);
  11. $this->setTable('bom_proceso');
  12. $this->setDisplayField('bom_id');
  13. $this->setPrimaryKey(['bom_id', 'proceso_id']);
  14. $this->belongsTo('Proceso', [
  15. 'foreignKey' => 'proceso_id',
  16. 'joinType' => 'INNER'
  17. ]);
  18. $this->belongsTo('Bom', [
  19. 'foreignKey' => 'bom_id',
  20. 'joinType' => 'INNER'
  21. ]);
  22. }
  23. /**
  24. * Default validation rules.
  25. *
  26. * @param \Cake\Validation\Validator $validator Validator instance.
  27. * @return \Cake\Validation\Validator
  28. */
  29. public function validationDefault(Validator $validator)
  30. {
  31. $validator
  32. ->numeric('time')
  33. ->allowEmpty('time');
  34. return $validator;
  35. }
  36. /**
  37. * Returns a rules checker object that will be used for validating
  38. * application integrity.
  39. *
  40. * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
  41. * @return \Cake\ORM\RulesChecker
  42. */
  43. public function buildRules(RulesChecker $rules)
  44. {
  45. $rules->add($rules->existsIn(['proceso_id'], 'Proceso'));
  46. $rules->add($rules->existsIn(['bom_id'], 'Bom'));
  47. return $rules;
  48. }

控制器视图功能

  1. public function view($id = null)
  2. {
  3. $bomProceso = $this->BomProceso->get($id, [
  4. 'contain' => ['Proceso', 'Bom']
  5. ]);
  6. $this->set('bomProceso', $bomProceso);
  7. }

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

  1. public function view($proceso_id = null, $bom_id=null)
  2. {
  3. $bomProceso = $this->BomProceso->get([$procesoID, $bomID], [
  4. 'contain' => ['Proceso', 'Bom']
  5. ]);
  6. $this->set('bomProceso', $bomProceso);
  7. }

但是,我现在看到的查询条件是 WHERE(BomProceso.bom_id = 1 AND BomProceso.proceso_id = NULL) 文件 routes.php 我可以在config文件夹中找到,如下所示
配置/routes.php

  1. use Cake\Core\Plugin;
  2. use Cake\Routing\RouteBuilder;
  3. use Cake\Routing\Router;
  4. use Cake\Routing\Route\DashedRoute;
  5. /**
  6. * The default class to use for all routes
  7. *
  8. * The following route classes are supplied with CakePHP and are appropriate
  9. * to set as the default:
  10. *
  11. * - Route
  12. * - InflectedRoute
  13. * - DashedRoute
  14. *
  15. * If no call is made to `Router::defaultRouteClass()`, the class used is
  16. * `Route` (`Cake\Routing\Route\Route`)
  17. *
  18. * Note that `Route` does not do any inflections on URLs which will result in
  19. * inconsistently cased URLs when used with `:plugin`, `:controller` and
  20. * `:action` markers.
  21. *
  22. */
  23. Router::defaultRouteClass(DashedRoute::class);
  24. Router::scope('/', function (RouteBuilder $routes) {
  25. /**
  26. * Here, we are connecting '/' (base path) to a controller called 'Pages',
  27. * its action called 'display', and we pass a param to select the view file
  28. * to use (in this case, src/Template/Pages/home.ctp)...
  29. */
  30. $routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
  31. /**
  32. * ...and connect the rest of 'Pages' controller's URLs.
  33. */
  34. $routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
  35. /**
  36. * Connect catchall routes for all controllers.
  37. *
  38. * Using the argument `DashedRoute`, the `fallbacks` method is a shortcut for
  39. * `$routes->connect('/:controller', ['action' => 'index'], ['routeClass' => 'DashedRoute']);`
  40. * `$routes->connect('/:controller/:action/*', [], ['routeClass' => 'DashedRoute']);`
  41. *
  42. * Any route class can be used with this method, such as:
  43. * - DashedRoute
  44. * - InflectedRoute
  45. * - Route
  46. * - Or your own route class
  47. *
  48. * You can remove these routes once you've connected the
  49. * routes you want in your application.
  50. */
  51. $routes->fallbacks(DashedRoute::class);
  52. });
  53. /**
  54. * Load all plugin routes. See the Plugin documentation on
  55. * how to customize the loading of plugin routes.
  56. */
  57. Plugin::routes();
nmpmafwu

nmpmafwu1#

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

  1. public function view($bom_id = null, $proceso_id = null)
  2. {
  3. $bomProceso = $this->BomProceso->get([$bom_id, $proceso_id], [
  4. 'contain' => ['Proceso', 'Bom']
  5. ]);
  6. $this->set('bomProceso', $bomProceso);
  7. }

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

相关问题