CMS Tutorial Add: Input Data Is Not Saved - how to troubleshoot?

Please check my codes below:

<h1>Add Visit</h1>
<?php
    echo $this->Form->create($visit);
    // Hard code the user for now.
    echo $this->Form->control('mbr_id', [
        'label' => 'Member ID',
        'type' => 'text'
    ]);
    echo $this->Form->button(__('Save Visit'));
    echo $this->Form->end();
?>

And this is the add controller code:

public function add()
  {
    $visit = $this->Visits->newEmptyEntity();
    if ($this->request->is('post')) {
      pr($this->request->getData());
      $visit = $this->Visits->patchEntity($visit, $this->request->getData());
      
      if ($this->Visits->save($visit)) {
        $this->Flash->success(__('Your visit has been saved.'));
        return $this->redirect(['action' => 'index']);
      }
      $this->Flash->error(__('Unable to add your visit.'));
    }
    $this->set('visit', $visit);
  }

The only input I have is the Member ID. On saving, the mbr_id field has 0 instead of a number I entered, example 200. The codes are based from the Cakephp v4 tutorial.

Our POST data is available in $this->request->getData() . You can use the pr() or debug() functions to print it out if you want to see what it looks like. To save our data, we first ‘marshal’ the POST data into an Article Entity. The Entity is then persisted using the ArticlesTable we created earlier.

The above statement if from the CakePHP 4 CMS tutorial. I would like to use the pr() and debug() functions for checking out the data. But where do I call these functions? in the controller files actions methods → like the add() function above ? → I tried but I did not see any data…

I’m not going to add links for now, for fear of being identified as a spammer by the forum again and then not be given access to this forum.

Do you have debugging enabled? If not, then pr and debug won’t produce any output. You’ve got it in the right spot. debug can be better than pr, because it more clearly indicates if the variable is false, null or blank.

  public function add()
  {
    $visit = $this->Visits->newEmptyEntity();
    if ($this->request->is('post')) {
      $visit = $this->Visits->patchEntity($visit, $this->request->getData());
      debug($visit);
      
      if ($this->Visits->save($visit)) {
        $this->Flash->success(__('Your visit has been saved.'));
        // return $this->redirect(['action' => 'index']);
      }
      $this->Flash->error(__('Unable to add your visit.'));
    }
    $this->set('visit', $visit);
  }

The result of the debub() is as follows:

object(App\Model\Entity\Visit) {

	'[new]' => true,
	'[accessible]' => [
		'*' => true,
		'id' => false,
		'mbr_id' => false
	],
	'[dirty]' => [],
	'[original]' => [],
	'[virtual]' => [],
	'[hasErrors]' => false,
	'[errors]' => [],
	'[invalid]' => [],
	'[repository]' => 'Visits'

}

I did not see the input value (example: 1000) from the add form for the member id. What do I check next?

Your accessible property has explicitly disallowed setting of mbr_id via patching.

That’s it. Thanks. Learned at least 2 topics here. Patch and accessible relationship and how to use debug() in controller.

1 Like

Where must debugging be enabled?

config/app.php should have a setting for it.

in Cake4x you’ll probably want to set that in config/app_local.php

You refer to 3.8. I found this in app_local.php for Cake 4:

debug’ => filter_var(env(‘DEBUG’, true), FILTER_VALIDATE_BOOLEAN),

by default ‘true’.