Passing Variable to the Method

What do you expect that $this->request->getData('147') is going to return? It should be $this->request->getData('id') if anything. But you shouldn’t need that at all, as $id should already be set from the URL. Does the code that was baked for you not work in some way?

$this->request->getData(‘147’) was a typo. I meant to use $this->request->getData(‘id’) but that throws an error as well.

Your Form “add” leads to the action “edit” in Mealplans Controller.
Your code is confusing. What are you trying to do?

i assume you want to get the user_id to the add action in your controller.
So your add template should look like this:

        <?= $this->Form->create($mealplan) ?>
        <fieldset>
            <legend><?= __('Add Mealplan') ?></legend>
            <?php
                echo $this->Form->control('name');
                //we want the user_id to be set to the id of the current logged in user, so we uncomment the next line (the line was vreated by the bake command)
                //echo $this->Form->control('user_id', ['options' => $users]);

                //and to send the user_id of the current logged in user to our controller we grab the user id and sent it woth an hidden form and a proper name to the controller
                echo $this->Form->hidden('user_id',['value'=>$this->request->getAttribute('identity')->id]);
            ?>
        </fieldset>
        <?= $this->Form->button(__('Submit')) ?>
        <?= $this->Form->end() ?>

and your add controller:

public function add()
    {
        $mealplan = $this->Mealplans->newEmptyEntity();
        if ($this->request->is('post')) {
            debug($this->request->getData()); //this is just to show, what data was sent from the template
            //as you can see, there is a variable "user_id" which has the value of the current logged in user
            $mealplan = $this->Mealplans->patchEntity($mealplan, $this->request->getData());
            debug($mealplan); //this is just to show the result of patching your Datasource
            //as you can see, the variable "user_id" is taken iver to your entity and the entity should have no errors and can be saved
            die();
            if ($this->Mealplans->save($mealplan)) {
                $this->Flash->success(__('The mealplan has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The mealplan could not be saved. Please, try again.'));
        }
        $users = $this->Mealplans->Users->find('list', ['limit' => 200]);
        $this->set(compact('mealplan', 'users'));
    }

Thanks, dirk. Eventually I do want to pass the user id to the method in the controller but as the first step I wanted to pass the mealplan id. This line of code that you wrote will probably only work for the user id because getAttribute takes identity as the parameter:
echo $this->Form->hidden('user_id',['value'=>$this->request->getAttribute('identity')->id]);
I googled getAttribute but all of the results showed getAttribute(‘identity’) and not a similar syntax for getting a non-user related variable.

in method add you can‘t pass a mealplan id, because it simply does not exist until you save the new data to your database, The id is accessible after saving the entity in the add method. then you can access the id like this:
$mealplan_id=$mealplan->id

In method edit the id is part of the entity. You call the method in a way like this: http://yourdomain.tld/mealplans/edit/2 , where the number 2 is the id of the mealplan you want to edit.
the controller checks if the id exists and gets all the mealplan data from the database table. this is done with

$mealplan = $this->Mealplans->get($id, [
‘contain’ => [],
]);

if you want to display the id in your edit form, just use echo $mealplan->id

you don‘t need to pass the id in a separate variable from your edit form to your edit method in mealplans controller, it is already in your entity.

you can see the difference in the entities when you write „debug($mealplan); die();“ right before the save command in edit or add method.
In the add method you will see, that your entity has no id and the key „new“ is set to true.
in the edit method you will see a id and the key „new“ is set to false.

This is so complicated. I looked at the Articles tutorial at CMS Tutorial - Creating the Articles Controller - 3.9 and it was too complicated also. I want to stop working on it. I wanted to just copy and paste from the articles tutorial but that’s not easily done either.

I’m going to just bake it and hope for the best.

that is a good idea.
In the end it‘s not as complicated as it may looks in the beginning.
Make small steps and you also will reach the goal.

Thanks for your help, dirk.

I decided to return to using localhost instead of my domain but now there’s a problem with that. I editted my login template but the changes don’t show up on the page. The views cache is empty but I can’t update my page.

I tried CTRL + SHIFT + R

I hadn’t posted on this thread as there was so much wrong (please don’t take offense) with the code. We obviously don’t want you to give up, but I remind you of my previous suggestion of looking for formal training. It would be unreasonable to assume that anyone has the free time to take you under their wing to help guide your understanding - so you need to decide.

As I see it, your options are: -

  1. Pay someone to do the development, and try to understand how it works from the provided code. Not recommended as a means of learning.
  2. Find a coach you can afford and know you will get along with. And, be patient. I am a programmer, I have a degree that took 5 years full-time at university. This is a highly skilled profession where the ground changes under our feet every year. So don’t expect too much of yourself.
  3. As mentioned, quit.

I suppose I am curious as to how this app is evidently aimed at a user base for a specific purpose, but also is a personal hobby project. It may help us if we know the goal. Is it for a charity that provides food, or a diet clinic, or even a hotel or aged care?

I agree with Jawfin.

the mealplan id does not exist until the mealplan is saved.
so you can not access it in your add template.
when the mealplan is successfully saved in your mealplan controller (add action) you can access the id with $mealplan_id=$this->$mealplan->id;

What are you trying to do, that you would need the id in the add action?

Yes, I realized my wrong reasoning after submitting my reply but I was away from home and couldn’t correct myself. What I want to do is detect the last submitted id and increment it by one. I’ve been leaning heavily on the forum up to now so I will write this code myself. So don’t provide the answer.

i will not answer, but you know, that your databse increments the id automatically, if the id field in your database is set to autoincrement?

I want to create a new id for the changes I’ve made and send it to the edit method. This will only work if the new id matches a record in the table. I want to create a new record so that the id is available for the edit method.

This is how I create an id to use in the edit method so that I can save a mealplan with the newest id:

$query = $this->Mealplans->find('list', [
'order' => ['Mealplans.created' => 'DESC']
]);

$id = $query->first();
$mealplan = $this->Mealplans->get($id, [
            'contain' => [],
        ]);

This is not race-condition safe and inherently dangerous. You will absolutely leak data to another user when concurrent users are logged in and creating meal plans.

Thanks for the input, Jawfin. Should I change it so that I’m getting the mealplan id for just the logged in user? I’m not sure what changes need to be made.