无法解决 cakephp 3 中的子父关联

Can't solve child parent association in cakephp 3

我有table与自身的亲子关系:

mysql> desc features;
+---------------------+-------------+------+-----+-------------------+----------------+
| Field               | Type        | Null | Key | Default           | Extra          |
+---------------------+-------------+------+-----+-------------------+----------------+
| id                  | int(11)     | NO   | PRI | NULL              | auto_increment |
| featureID           | varchar(45) | NO   |     | NULL              |                |
| probeID             | int(11)     | YES  |     | NULL              |                |
| shortName           | varchar(45) | NO   |     | NA                |                |
| start               | int(11)     | NO   |     | NULL              |                |
| stop                | int(11)     | NO   |     | NULL              |                |
| strand              | int(11)     | NO   |     | NULL              |                |
| curatedManually     | varchar(45) | NO   |     | NA                |                |
| created             | timestamp   | NO   |     | CURRENT_TIMESTAMP |                |
| descriptions_id     | int(11)     | YES  | MUL | NULL              |                |
| features_types_id   | int(11)     | NO   | MUL | NULL              |                |
| chromosomes_id      | int(11)     | NO   | MUL | NULL              |                |
| species_id          | int(11)     | NO   | MUL | NULL              |                |
| strains_id          | int(11)     | NO   | MUL | NULL              |                |
| parents_features_id | int(11)     | YES  | MUL | NULL              |                |
+---------------------+-------------+------+-----+-------------------+----------------+

相应的字段是parents_features_id和id,因为一个特征可以有"child"-特征或"parent"-特征。与该字段建立了外键关系。

KEY `fk_features_Features1_idx` (`parents_features_id`),
  CONSTRAINT `fk_features_Features1` FOREIGN KEY (`parents_features_id`) REFERENCES `features` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,

我用 "cake bake all features" 创建了所有必要的模型、控制器等

当我打开功能页面时,我只收到错误消息 "Features is not associated with ParentsFeatures"

我尝试通过交换自动原码解决

$this->belongsTo('Features', [
            'foreignKey' => 'parents_features_id'

in Model/Table/FeaturesTable.php 为这个关系通过下面的代码:

 $this->belongsTo('ParentsFeatures', [
        'className' => 'Features',
        'foreignKey' => 'parents_features_id'
    ]);
    $this->hasMany('ChildFeatures', [
        'className' => 'Features',
        'foreignKey' => 'parents_features_id'

但是我收到错误消息 "Features is not associated with Features"

我有点卡在这里,非常感谢任何帮助解决这个问题。

祝一切顺利 纳丁

更改 table 中的关联不会改变烘焙控制器查询数据的方式,您也必须更改它们,或者重新烘焙您的代码。

但是,除非您开始遵循约定,即命名外键列 parent_id,否则这将永远无法正确烘焙,只有这样烘焙才能创建正确的关联(将被命名为 ParentFeaturesChildFeatures),当你这样做时,考虑也更改其他列名称(小写下划线)。

也就是说,在重命名索引、创建和删除外键约束等之间的某处可能会出现错误(无法查明),然后导致错误的关联名称用于 contain 在控制器操作的 find() 调用中,即烘焙将生成类似

的东西
'contain' => ['Features']

而不是

'contain' => ['ParentFeatures']

它用于烘焙中的关联 table class.

但是我现在无法可靠地重现它。如果这是您遇到的情况,您可能需要 report this as an issue over at GitHub.

如果重新烘焙无法解决问题,请手动检查控制器操作中的 find() 调用并将包含的关联更改为 ParentFeatures