0
votes

I'm testing CakePHP 3.0-RC1 for possible use on a new project. After installing, configuring and creating two (yes, two) database tables, I ran 'bake all' against both tables.

After dealing with the spurious foreign key references in the model (a foreign key defined as the primary key referencing itself? C'mon, now, Bakers!) I am hitting this error:

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

Not my code. ;)

The offending table definition:

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

The IsoCountryCodesController index method, as generated by bake:

/**
* Index method
*
* @return void
*/
public function index()
{
    $this->paginate = [
        'contain' => ['IsoCountryCodes']
    ];
    $this->set('isoCountryCodes',    $this->paginate($this->IsoCountryCodes));
    $this->set('_serialize', ['isoCountryCodes']);
}

And the initialize method from 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']);
}

with the reflexive foreign key commented out.

This behaviour holds for both tables.

CakePHP 2.5 works correctly with the same database table definitions.

1
Line 193? That doesn't even exist. Anyways, you are working against the conventions, so you shouldn't be too susprised about having to deal with problems. The problematic primary key name (you might want to report this as an isse over at GitHub, or maybe even fix it yourself in a PR) and the inferred association and containment (you should remove that) aside, it works fine for me, no such error. Also bake doesn't generate a set() call with _serialize for me, but I'm using a recent dev snapshot, maybe you should give that a try too.ndm

1 Answers

0
votes

Remove the 'contain' part in the paginate array. You don't have any association to IsoCountryCodes

The reason you got that result when baking is that bake is a conventions-based too. It can only try doing its best based on the stablished conventions. One of those conventions is that any column ending in _id is a foreignKey to another table.

Another convention is that the primary keys of tables should be named id. When not following the conventions you will need to help bake figuring out or fixing its guessing errors in the code it generates.