问题
I've baked a legacy application using CakePHP 3.7
The database contains 3 tables as follows: regulations
, groups
, filters
. The hierarchy as far as the application goes is:
- Regulations
- Groups
- Filters
- Groups
The table schemas are as follows:
mysql> describe regulations;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| label | varchar(255) | NO | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
4 rows in set (0.09 sec)
mysql> describe groups;
+---------------+-----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-----------------+------+-----+---------+----------------+
| id | int(4) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| regulation_id | int(4) unsigned | NO | MUL | NULL | |
| label | varchar(255) | NO | | NULL | |
+---------------+-----------------+------+-----+---------+----------------+
4 rows in set (0.03 sec)
mysql> describe filters;
+----------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+----------------+
| id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| label | text | NO | | NULL | |
| group_id | int(4) unsigned | NO | MUL | NULL | |
+----------+----------------------+------+-----+---------+----------------+
So when I baked the application I got the following Table class relationships. All are inside the initialize()
method:
// RegulationsTable.php
$this->hasMany('Groups', [
'foreignKey' => 'regulation_id'
]);
// GroupsTable.php
$this->belongsTo('Regulations', [
'foreignKey' => 'regulation_id',
'joinType' => 'INNER'
]);
$this->hasMany('Filters', [
'foreignKey' => 'group_id'
]);
// FiltersTable.php
$this->belongsTo('Groups', [
'foreignKey' => 'group_id',
'joinType' => 'INNER'
]);
In my mind this seems correct because those are the relationships as far as the schema goes.
However, when I try and do this in a Controller:
$regulations = TableRegistry::getTableLocator()->get('Regulations');
$data = $regulations->find('all')->contain(['Groups', 'Filters']);
$data = $data->toArray();
debug($data);
It's giving an error:
The Filters association is not defined on Regulations.
Well, obviously. Because the association for that is handled through the Groups (GroupsTable.php
) model. There is no direct relationship between Filters and Regulations; it must go via Groups.
Please can someone advise what is wrong with the baked Models so that this won't produce an error?
For background info: the use case is getting a list of the data (specifically the label
from each table) in a hierarchical format (Regulations --> Groups --> Filters).
来源:https://stackoverflow.com/questions/56563186/cakephp-3-association-is-not-defined-even-though-it-appears-to-be