Hello guys,
I need help for my timesheets app model …
What I want to do is an app that allows you to save your times for every day.
I built this model :
Note : I added the hasMany relation from timesheets to timesheets_tasks model because I didn’t manage to access days table by another way
At the end I am not sure the belongsToMany from timesheets to tasks is really appropriate in this situation …
I have 2 questions :
- Does this model sound good to you ?
- How to save days ?
Because when I save this :
\src\Controller\TimesheetsController.php (line 115)
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'
}
I doesn’t save days … The controller :
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'));
}
I don’t have any error, but days are not saved …