<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);
}