Ultra Slow Page Loads

My page frequently reaches the maximum execution time and doesn’t load. Can anyone see by my controller code why it loads so slowly? I am using cakephp version 4.

public function add()
    {
$mealplan = $this->Mealplans->newEmptyEntity();
if ($this->request->is('post')) {
$mealplan = $this->Mealplans->patchEntity($mealplan, $this->request->getData());
if ($this->Mealplans->save($mealplan)) {
$result=$this->Mealplans->save($mealplan);
$id=$result->id;
$this->Flash->success(__('The mealplan has been saved.'));
return $this->redirect(['action' => 'edit/' . $id]); 
            }
$this->Flash->error(__('The mealplan could not be saved. Please, try again.'));
        }
$users = $this->Mealplans->Users->find('list', ['limit' => 2]);
$this->loadModel('Tiles');
$this->set('tiles', $this->Tiles->find('all', ['limit' => 24]));
$this->set(compact('mealplan', 'users'));
    }

Nothing seems obviously wrong with this from a timing perspective. The double-save is pointless, though; when the save succeeds, the entity is updated. Delete this:

$result=$this->Mealplans->save($mealplan);
$id=$result->id;

and use this:

return $this->redirect(['action' => 'edit', $mealplan->id]);

(Also note that this is passing the id as a parameter in the array, instead of the thing you’ve got where you’re concatenating to the action string. What you’re doing is very non-standard, and will likely cause you problems eventually.)

Thanks for the input, Zuluru.

in 3.1 save() return true/false so above should not work (I don’t know 4.x version) When I need ID after save I get it from entity, in your case $id = $mealplan->id;
By the way in v4 book is the same approach: Saving Data - 4.x

ps. about time: check timers in debug bar - “sql” and “timer”. You can also add echo TIme::now(); die; in different places and check manually which part take longest time. php ob_flush() and flush() is also useful for this kind of test.

I deleted the reference to an external stylesheet that never loaded and now my page loads fast.