CakePHP 3.0-RC1 从 Baked 控制器中的索引方法抛出致命错误
CakePHP 3.0-RC1 throws fatal error from index method in Baked controller
我正在测试 CakePHP 3.0-RC1 是否可以用于新项目。在安装、配置和创建两个(是的,两个)数据库 table 之后,我 运行 'bake all' 针对两个 table。
在处理模型中的虚假外键引用后(定义为引用自身的主键的外键?来吧,Bakers!)我遇到了这个错误:
Error: Method Cake\ORM\Query::__toString() must not throw an exception
File /srv/www/htdocs/wmcb/cake/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php
Line: 193
不是我的代码。 ;)
违规的 table 定义:
-- -----------------------------------------------------
-- Table `ISOCountryCodes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `iso_country_codes` (
`iso_country_code_id` VARCHAR(4) CHARACTER SET utf8 NOT NULL ,
`iso_country_name` VARCHAR(64) CHARACTER SET utf8 NOT NULL ,
`iso_country_name_french` VARCHAR(64) CHARACTER SET utf8 NOT NULL ,
PRIMARY KEY (`iso_country_code_id`)
)
ENGINE = InnoDB
DEFAULT CHARSET = utf8
COMMENT = 'Look-up table of ISO 3166-1 Country Names and Codes'
;
IsoCountryCodesController 索引方法,由 bake 生成:
/**
* Index method
*
* @return void
*/
public function index()
{
$this->paginate = [
'contain' => ['IsoCountryCodes']
];
$this->set('isoCountryCodes', $this->paginate($this->IsoCountryCodes));
$this->set('_serialize', ['isoCountryCodes']);
}
以及 IsoCountryCodesTable.php 中的初始化方法:
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
$this->table('iso_country_codes');
$this->displayField('iso_country_code_id');
$this->primaryKey('iso_country_code_id');
// $this->belongsTo('IsoCountryCodes', ['foreignKey' => iso_country_code_id']);
}
自反外键被注释掉。
此行为对 table 都适用。
CakePHP 2.5 使用相同的数据库 table 定义可以正常工作。
删除 paginate
数组中的 'contain'
部分。您与 IsoCountryCodes
没有任何关联
你在烘焙时得到这个结果的原因是烘焙也是基于约定的。它只能根据既定的惯例尽力而为。其中一个约定是任何以 _id
结尾的列都是另一个 table 的外键。
另一个约定是table的主键应该命名为id
。如果不遵循约定,您将需要帮助 bake 找出或修复其生成的代码中的猜测错误。
我正在测试 CakePHP 3.0-RC1 是否可以用于新项目。在安装、配置和创建两个(是的,两个)数据库 table 之后,我 运行 'bake all' 针对两个 table。
在处理模型中的虚假外键引用后(定义为引用自身的主键的外键?来吧,Bakers!)我遇到了这个错误:
Error: Method Cake\ORM\Query::__toString() must not throw an exception File /srv/www/htdocs/wmcb/cake/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php Line: 193
不是我的代码。 ;)
违规的 table 定义:
-- -----------------------------------------------------
-- Table `ISOCountryCodes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `iso_country_codes` (
`iso_country_code_id` VARCHAR(4) CHARACTER SET utf8 NOT NULL ,
`iso_country_name` VARCHAR(64) CHARACTER SET utf8 NOT NULL ,
`iso_country_name_french` VARCHAR(64) CHARACTER SET utf8 NOT NULL ,
PRIMARY KEY (`iso_country_code_id`)
)
ENGINE = InnoDB
DEFAULT CHARSET = utf8
COMMENT = 'Look-up table of ISO 3166-1 Country Names and Codes'
;
IsoCountryCodesController 索引方法,由 bake 生成:
/**
* Index method
*
* @return void
*/
public function index()
{
$this->paginate = [
'contain' => ['IsoCountryCodes']
];
$this->set('isoCountryCodes', $this->paginate($this->IsoCountryCodes));
$this->set('_serialize', ['isoCountryCodes']);
}
以及 IsoCountryCodesTable.php 中的初始化方法:
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
$this->table('iso_country_codes');
$this->displayField('iso_country_code_id');
$this->primaryKey('iso_country_code_id');
// $this->belongsTo('IsoCountryCodes', ['foreignKey' => iso_country_code_id']);
}
自反外键被注释掉。
此行为对 table 都适用。
CakePHP 2.5 使用相同的数据库 table 定义可以正常工作。
删除 paginate
数组中的 'contain'
部分。您与 IsoCountryCodes
你在烘焙时得到这个结果的原因是烘焙也是基于约定的。它只能根据既定的惯例尽力而为。其中一个约定是任何以 _id
结尾的列都是另一个 table 的外键。
另一个约定是table的主键应该命名为id
。如果不遵循约定,您将需要帮助 bake 找出或修复其生成的代码中的猜测错误。