无法编辑与 CakePHP 的 HABTM 关联中的属性
Can't edit an attribute in a HABTM association with CakePHP
这是我的数据库的一个片段。 Model
我使用 CakePHP 3.0 Bake 命令来创建控制器、模型和视图。如您所见,我有一个 HABTM 关联,但它也有一个属性。我可以使用 BOM 视图和过程视图插入数据。但是,当我尝试编辑与这些东西相关的时间时,我收到了消息
Record not found in table "bom_proceso" with primary key ['1']
我需要编辑那个属性,但我不知道如何处理这个错误或者我是否做错了什么。
BomProcesoTable 型号
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)
我可以在 config 文件夹中找到的文件 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();
生成的控制器源代码有误。它是为使用单个主键而生成的,但应该是复合键。
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
。
这是我的数据库的一个片段。 Model
我使用 CakePHP 3.0 Bake 命令来创建控制器、模型和视图。如您所见,我有一个 HABTM 关联,但它也有一个属性。我可以使用 BOM 视图和过程视图插入数据。但是,当我尝试编辑与这些东西相关的时间时,我收到了消息
Record not found in table "bom_proceso" with primary key ['1']
我需要编辑那个属性,但我不知道如何处理这个错误或者我是否做错了什么。
BomProcesoTable 型号
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)
我可以在 config 文件夹中找到的文件 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();
生成的控制器源代码有误。它是为使用单个主键而生成的,但应该是复合键。
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
。