Cakephp3三个表

时间:2016-04-02 18:08:07

标签: php cakephp cakephp-3.0

我有三张桌子:

 - 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_tests 的单独表格。

以下是 samples_tests 表的结构:

+----+-----------+---------+
| ID | sample_id | test_id |
+----+-----------+---------+
| 1  | 2         | 4       |
+----+-----------+---------+
| 2  | 3         | 4       |
+----+-----------+---------+
| 3  | 3         | 2       |
+----+-----------+---------+

sample_id和test_id都是外键。

以下是 SamplesTable.php

中的关联
$this->belongsToMany('Tests', [
            'foreignKey' => 'sample_id',
            'targetForeignKey' => 'test_id',
            'joinTable' => 'samples_tests'
        ]);

以下是 TestsTable.php

中的关联
$this->belongsToMany('Samples', [
            'foreignKey' => 'test_id',
            'targetForeignKey' => 'sample_id',
            'joinTable' => 'samples_tests',
            'dependent' => true,
            'cascadeCallbacks' => true
        ]);

它的工作非常好,但现在我必须介绍结果部分,我现在停留在如何定义关联。

任何帮助将不胜感激。

更新

根据@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();

但这样做会增加以下复杂性:

当我添加新样本时,我还从多选下拉列表中选择测试。但它也要求结果才能成功保存样品。

我的目的是通过测试保存样本,并在以后随时添加结果。

1 个答案:

答案 0 :(得分:0)

结果和测试具有一对一的关系。

将test_id字段添加到结果表

然后在ResultsTable初始化方法中设置关系:

   document.getElementById(id)

http://book.cakephp.org/3.0/en/orm/associations.html#hasone-associations

您可以在samples_tests表中存储其他数据:

$this->hasOne('Results');

http://book.cakephp.org/3.0/en/orm/associations.html#using-the-through-option