关于cakephp3中belongtomany关联的问题

pqwbnv8z  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(362)

我需要帮助我的时间表应用程序模型…
我想做的是一个应用程序,让你节省你的时间为每一天。
我建立了这个模型:

注意:我添加了从时间表到时间表任务模型的hasmany关系,因为我无法通过其他方式访问days表
最后,我不确定从时间表到任务的归属在这种情况下是否真的合适…
我有两个问题:
你觉得这个型号好吗?如何节省时间?因为当我保存这个时:

object(App\Model\Entity\Timesheet) {

    'id' => (int) 2,
    'user_id' => (int) 1,
    'week_nb' => (int) 16,
    'year' => (int) 2018,
    'validated' => false,
    'created' => object(Cake\I18n\FrozenTime) {

        'time' => '2018-04-02T09:25:29+00:00',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },
    'modified' => object(Cake\I18n\FrozenTime) {

        'time' => '2018-04-28T22:17:29+00:00',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },
    'tasks' => [
        (int) 0 => object(App\Model\Entity\Task) {

            'id' => (int) 1,
            'name' => 'Atelier 1 - Compta',
            'scope' => 'global',
            'user_id' => null,
            'project_id' => (int) 1,
            'created' => object(Cake\I18n\FrozenTime) {

                'time' => '2018-04-01T15:19:44+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            'modified' => object(Cake\I18n\FrozenTime) {

                'time' => '2018-04-01T15:19:44+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'Tasks'

        },
        (int) 1 => object(App\Model\Entity\Task) {

            'id' => (int) 2,
            'name' => 'Atelier 2 - Achats',
            'scope' => 'global',
            'user_id' => null,
            'project_id' => (int) 1,
            'created' => object(Cake\I18n\FrozenTime) {

                'time' => '2018-04-01T15:20:13+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            'modified' => object(Cake\I18n\FrozenTime) {

                'time' => '2018-04-01T15:20:13+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'Tasks'

        }
    ],
    'timesheets_tasks' => [
        (int) 0 => object(App\Model\Entity\TimesheetsTask) {

            'id' => (int) 13,
            'timesheet_id' => (int) 2,
            'task_id' => (int) 1,
            'created' => object(Cake\I18n\FrozenTime) {

                'time' => '2018-04-28T22:04:45+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            'modified' => object(Cake\I18n\FrozenTime) {

                'time' => '2018-04-28T22:04:45+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            'days' => [
                (int) 0 => object(App\Model\Entity\Day) {

                    'date' => object(DateTime) {
                        date => '2018-04-16 22:17:40.643236'
                        timezone_type => (int) 3
                        timezone => 'UTC'
                    },
                    '[new]' => true,
                    '[accessible]' => [
                        '*' => true
                    ],
                    '[dirty]' => [
                        'date' => true
                    ],
                    '[original]' => [],
                    '[virtual]' => [],
                    '[errors]' => [],
                    '[invalid]' => [],
                    '[repository]' => 'Days'

                },
                (int) 1 => object(App\Model\Entity\Day) {

                    'date' => object(DateTime) {
                        date => '2018-04-17 22:17:40.647586'
                        timezone_type => (int) 3
                        timezone => 'UTC'
                    },
                    '[new]' => true,
                    '[accessible]' => [
                        '*' => true
                    ],
                    '[dirty]' => [
                        'date' => true
                    ],
                    '[original]' => [],
                    '[virtual]' => [],
                    '[errors]' => [],
                    '[invalid]' => [],
                    '[repository]' => 'Days'

                },
                (int) 2 => object(App\Model\Entity\Day) {

                    'date' => object(DateTime) {
                        date => '2018-04-18 22:17:40.647655'
                        timezone_type => (int) 3
                        timezone => 'UTC'
                    },
                    '[new]' => true,
                    '[accessible]' => [
                        '*' => true
                    ],
                    '[dirty]' => [
                        'date' => true
                    ],
                    '[original]' => [],
                    '[virtual]' => [],
                    '[errors]' => [],
                    '[invalid]' => [],
                    '[repository]' => 'Days'

                },
                (int) 3 => object(App\Model\Entity\Day) {

                    'date' => object(DateTime) {
                        date => '2018-04-19 22:17:40.647705'
                        timezone_type => (int) 3
                        timezone => 'UTC'
                    },
                    '[new]' => true,
                    '[accessible]' => [
                        '*' => true
                    ],
                    '[dirty]' => [
                        'date' => true
                    ],
                    '[original]' => [],
                    '[virtual]' => [],
                    '[errors]' => [],
                    '[invalid]' => [],
                    '[repository]' => 'Days'

                },
                (int) 4 => object(App\Model\Entity\Day) {

                    'date' => object(DateTime) {
                        date => '2018-04-20 22:17:40.647754'
                        timezone_type => (int) 3
                        timezone => 'UTC'
                    },
                    '[new]' => true,
                    '[accessible]' => [
                        '*' => true
                    ],
                    '[dirty]' => [
                        'date' => true
                    ],
                    '[original]' => [],
                    '[virtual]' => [],
                    '[errors]' => [],
                    '[invalid]' => [],
                    '[repository]' => 'Days'

                },
                (int) 5 => object(App\Model\Entity\Day) {

                    'date' => object(DateTime) {
                        date => '2018-04-21 22:17:40.647796'
                        timezone_type => (int) 3
                        timezone => 'UTC'
                    },
                    '[new]' => true,
                    '[accessible]' => [
                        '*' => true
                    ],
                    '[dirty]' => [
                        'date' => true
                    ],
                    '[original]' => [],
                    '[virtual]' => [],
                    '[errors]' => [],
                    '[invalid]' => [],
                    '[repository]' => 'Days'

                },
                (int) 6 => object(App\Model\Entity\Day) {

                    'date' => object(DateTime) {
                        date => '2018-04-22 22:17:40.647838'
                        timezone_type => (int) 3
                        timezone => 'UTC'
                    },
                    '[new]' => true,
                    '[accessible]' => [
                        '*' => true
                    ],
                    '[dirty]' => [
                        'date' => true
                    ],
                    '[original]' => [],
                    '[virtual]' => [],
                    '[errors]' => [],
                    '[invalid]' => [],
                    '[repository]' => 'Days'

                }
            ],
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [
                'days' => true
            ],
            '[original]' => [
                'days' => []
            ],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'TimesheetsTasks'

        }
    ],
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [
        'tasks' => true
    ],
    '[original]' => [
        'tasks' => [
            (int) 0 => object(App\Model\Entity\Task) {

                'id' => (int) 1,
                'name' => 'Atelier 1 - Compta',
                'scope' => 'global',
                'user_id' => null,
                'project_id' => (int) 1,
                'created' => object(Cake\I18n\FrozenTime) {

                    'time' => '2018-04-01T15:19:44+00:00',
                    'timezone' => 'UTC',
                    'fixedNowTime' => false

                },
                'modified' => object(Cake\I18n\FrozenTime) {

                    'time' => '2018-04-01T15:19:44+00:00',
                    'timezone' => 'UTC',
                    'fixedNowTime' => false

                },
                '_joinData' => object(App\Model\Entity\TimesheetsTask) {

                    'id' => (int) 13,
                    'timesheet_id' => (int) 2,
                    'task_id' => (int) 1,
                    'created' => object(Cake\I18n\FrozenTime) {

                        'time' => '2018-04-28T22:04:45+00:00',
                        'timezone' => 'UTC',
                        'fixedNowTime' => false

                    },
                    'modified' => object(Cake\I18n\FrozenTime) {

                        'time' => '2018-04-28T22:04:45+00:00',
                        'timezone' => 'UTC',
                        'fixedNowTime' => false

                    },
                    '[new]' => false,
                    '[accessible]' => [
                        '*' => true
                    ],
                    '[dirty]' => [],
                    '[original]' => [],
                    '[virtual]' => [],
                    '[errors]' => [],
                    '[invalid]' => [],
                    '[repository]' => 'TimesheetsTasks'

                },
                '[new]' => false,
                '[accessible]' => [
                    '*' => true
                ],
                '[dirty]' => [],
                '[original]' => [],
                '[virtual]' => [],
                '[errors]' => [],
                '[invalid]' => [],
                '[repository]' => 'Tasks'

            }
        ]
    ],
    '[virtual]' => [],
    '[errors]' => [],
    '[invalid]' => [],
    '[repository]' => 'Timesheets'

}

我不节省时间…控制器:

public function add()
{
    $timesheet = $this->Timesheets->newEntity();
    if ($this->request->is(['patch', 'post', 'put'])) {
        if(!is_null($this->request->getData('id'))) {
            $timesheet = $this->Timesheets->get($this->request->getData('id'), ['contain' => ['TimesheetsTasks' => ['Days'], 'Tasks']]);
        }

        // Here I "create" 7 days entities if a new timesheet_task has been added
        $this->initEmptyTasks($timesheet);

        $timesheet = $this->Timesheets->patchEntity($timesheet, $this->request->getData(), ['associated' => ['TimesheetsTasks' => ['associated' => ['Days']], 'Tasks']]);

        if ($result = $this->Timesheets->save($timesheet)) {
            $this->Flash->success(__('The timesheet has been saved.'));
            return $this->redirect($this->referer());
        }
        $this->Flash->error(__('The timesheet could not be saved. Please, try again.'));
        return $this->redirect($this->referer());
    }
    $users = $this->Timesheets->Users->find('list', ['limit' => 200]);
    $this->set(compact('timesheet', 'users'));
}

我没有任何错误,但天是不会保存…
事先谢谢你的帮助。

oiopk7p5

oiopk7p51#

天哪,我找到了根本原因…
我在修改实体,而不是用数据修补实体…
但现在我有一个问题:
你能有一个btm透视表与另一个表有很多关系吗?
因为我可以为additionnal字段(这里是comment字段)保存\u joindata,但不能为hasmany relation(days表)保存
在调用patchentity方法之前,下面是我的$数据:

[
    'id' => '2',
    'week_nb' => '16',
    'year' => '2018',
    'tasks' => [
        (int) 0 => [
            'id' => (int) 1,
            '_joinData' => [
                'comment' => 'Hihi tache 1 new',
                'days' => [
                    (int) 0 => [
                        'timesheet_task_id' => (int) 35,
                        'ts_date' => object(DateTime) {
                            date => '2018-04-16 19:04:45.150726'
                            timezone_type => (int) 3
                            timezone => 'Europe/Paris'
                        }
                    ]
                ]
            ]
        ],
        (int) 1 => [
            'id' => (int) 2,
            '_joinData' => [
                'comment' => 'Hihi tache 2'
            ]
        ]
    ]
]

以下是调用patchentity方法后的数据:

$timesheet = $this->Timesheets->patchEntity($timesheet, $data, ['associated' => ['Tasks._joinData.Days']]);

联合数据部分:

'_joinData' => object(App\Model\Entity\TimesheetsTask) {

    'id' => (int) 35,
    'timesheet_id' => (int) 2,
    'task_id' => (int) 1,
    'comment' => 'Hihi tache 1 new',
    'created' => object(Cake\I18n\FrozenTime) {

        'time' => '2018-05-01T18:39:17+02:00',
        'timezone' => 'Europe/Paris',
        'fixedNowTime' => false

    },
    'modified' => object(Cake\I18n\FrozenTime) {

        'time' => '2018-05-01T18:58:55+02:00',
        'timezone' => 'Europe/Paris',
        'fixedNowTime' => false

    },
    'days' => [
        (int) 0 => object(App\Model\Entity\Day) {

            'timesheet_task_id' => (int) 35,
            'ts_date' => object(DateTime) {
                date => '2018-04-16 19:02:01.023189'
                timezone_type => (int) 3
                timezone => 'Europe/Paris'
            },
            '[new]' => true,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [
                'timesheet_task_id' => true,
                'ts_date' => true
            ],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'Days'

        }
    ],
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [
        'days' => true
    ],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[invalid]' => [],
    '[repository]' => 'TimesheetsTasks'

},

它看起来像cakephpis一样将我的日记录转换成一个日实体,但现在的结果是一样的,它不能保存。

相关问题