Cakephp3三个表的关联
我有三张桌子:Cakephp3三个表的关联,php,cakephp,cakephp-3.0,Php,Cakephp,Cakephp 3.0,我有三张桌子: - Samples - Tests - Results 我的目标是在他们之间建立联系,以便: 一个样本可以有多个测试,一个测试可以有一个结果。 (一个样本有许多测试,另一个样本也可以有相同的测试,另一方面,一个测试有一个结果,另一个样本的相同测试可能有不同的结果。) 表示例: 这是我的示例表: +----+---------+-------+ | ID | Name | Type | +----+---------+-------+ | 1 | Sample1
- Samples
- Tests
- Results
我的目标是在他们之间建立联系,以便:
一个样本可以有多个测试,一个测试可以有一个结果。
(一个样本有许多测试,另一个样本也可以有相同的测试,另一方面,一个测试有一个结果,另一个样本的相同测试可能有不同的结果。)
表示例: 这是我的示例表:
+----+---------+-------+
| ID | Name | Type |
+----+---------+-------+
| 1 | Sample1 | Type1 |
+----+---------+-------+
| 2 | Sample2 | Type2 |
+----+---------+-------+
这是我的测试表:
+----+---------+
| ID | Name |
+----+---------+
| 1 | Test1 |
+----+---------+
| 2 | Test2 |
+----+---------+
这是我的结果表:
+----+-------+
| ID | Value |
+----+-------+
| 1 | 1.22 |
+----+-------+
| 2 | 909 |
+----+-------+
到目前为止我所做的:
我已经组合了样本和测试表,并创建了一个名为samples\u tests的单独表
以下是样本的结构\u测试表:
+----+-----------+---------+
| ID | sample_id | test_id |
+----+-----------+---------+
| 1 | 2 | 4 |
+----+-----------+---------+
| 2 | 3 | 4 |
+----+-----------+---------+
| 3 | 3 | 2 |
+----+-----------+---------+
样本id和测试id都是外键
这是我在SamplesTable.php中的关联
下面是我在TestsTable.php中的关联
它工作得很好,但是现在我必须介绍结果部分,我现在正被困在如何定义关联上
任何帮助都将不胜感激
更新
根据@CbNrZvWd的建议,我尝试了以下方法:
$table = $this->table('samples_tests_results', ['id' => false, 'primary_key' => ['sample_id', 'test_id', 'result_id']]);
$table
->addColumn('sample_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => false,
])
->addColumn('test_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => false,
])
->addColumn('result_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => false,
])
->addIndex(
[
'result_id',
]
)
->addIndex(
[
'sample_id',
]
)
->addIndex(
[
'test_id',
]
)
->create();
$this->table('samples_tests_results')
->addForeignKey(
'result_id',
'results',
'id',
[
'update' => 'RESTRICT',
'delete' => 'RESTRICT'
]
)
->addForeignKey(
'sample_id',
'samples',
'id',
[
'update' => 'RESTRICT',
'delete' => 'RESTRICT'
]
)
->addForeignKey(
'test_id',
'tests',
'id',
[
'update' => 'RESTRICT',
'delete' => 'RESTRICT'
]
)
->update();
但这样做会增加以下复杂性:
当我添加新样本时,我还从多选下拉列表中选择测试。但它也要求结果,以便成功保存样本
我所做的是用测试保存样本,并在以后需要时添加结果 结果和测试有一对一的关系 将测试id字段添加到结果表中 然后在ResultsTable initialize方法中设置关系:
$this->hasOne('Results');
您还可以在samples_tests表中存储其他数据:
ID |样本|测试|结果| ID
如何做到这一点,结果将使这句话成为事实:“一个测试有一个结果,而另一个样本的相同测试可能有不同的结果。”?哦,我忽略了这一部分。那么也许一个样本测试结果表就可以了?不,我也试过了,但是cakephp认为样本测试是一个表,结果是另一个表。你可以告诉cake使用哪个表:例如:在MyTable->initialize()中做$this->table('my_super_table');
$table = $this->table('samples_tests_results', ['id' => false, 'primary_key' => ['sample_id', 'test_id', 'result_id']]);
$table
->addColumn('sample_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => false,
])
->addColumn('test_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => false,
])
->addColumn('result_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => false,
])
->addIndex(
[
'result_id',
]
)
->addIndex(
[
'sample_id',
]
)
->addIndex(
[
'test_id',
]
)
->create();
$this->table('samples_tests_results')
->addForeignKey(
'result_id',
'results',
'id',
[
'update' => 'RESTRICT',
'delete' => 'RESTRICT'
]
)
->addForeignKey(
'sample_id',
'samples',
'id',
[
'update' => 'RESTRICT',
'delete' => 'RESTRICT'
]
)
->addForeignKey(
'test_id',
'tests',
'id',
[
'update' => 'RESTRICT',
'delete' => 'RESTRICT'
]
)
->update();
$this->hasOne('Results');