How do I access the fields in my controller?

I am using cakephp version 4. I have a table called mealplans and it has a couple of fields named event_date and end_date. The dates are in the wrong format and I want to change this in the controller. So I try to get the fields out of the table like this:

$this->set(['event_date' => $this->event_date,'end_date' => $this->end_date,]);	

I get the error:

Undefined property: CalendarsController::$event_date
Undefined property: CalendarsController::$end_date

How do I access the fields in my controller?

these fields are in your entity. you can access the fields in your controller like this:

if you want to change them try this: $mealplan->event_date = NEWVALUE;

“$this->set” is used to pass the variables from your controller to your form. In your case you just need to pass the entity “mealplan” to your form and can access all entitiy fields in your form.

Note that changing the format is something that almost always should be done in the output step (i.e. your template), not the controller. No offence, but many of the questions you’re asking here show a lack of understanding of core concepts. It’s great that you’re learning, and I can appreciate learning from doing instead of reading (I learn best that way myself), but I think you might be well served by going back over some of the early chapters of the cookbook; you’ll probably find a lot of things “click” and help you to understand problems that you’ve run into.

I thought about doing it in the template but I decided it was too much code and would mess up my HTML.

As for my uninformed questions, that’s a result of not being able to read. I can’t study the tutorial because I have very low reading comprehension as a result of the voices that I hear.

Thanks, dirk. After I added $mealplan = $this->Mealplans->newEmptyEntity(); it didn’t show an error. Then I tried to do

		$mealplan->$end_date =  date_create($end_date);
		$mealplan->$end_date = date_format($end_date,"Y-m-d");

and I got the error: Cannot set an empty field. But when I do:

		$end_date =  date_create($end_date);
		$end_date = date_format($end_date,"Y-m-d");

the changes to the date format aren’t reflected in the template because I declare the variables in the template like this:

	$event_date = $mealplan->event_date; 
	$end_date = $mealplan->end_date;

I tried echoing the variables without getting them from the database in my template but I got a bunch of data that was called $dataForView. I tried setting the date format in the template instead of the controller but I got the same errors.

I am going to try setting the validators in

public function validationDefault(Validator $validator): Validator

in my table.

The validators are already set in my table so that’s not the problem.

I think the problem is that I’m setting the mealplan to an empty entity with
$mealplan = $this->Mealplans->newEmptyEntity();

I’m reading about Accessors & Mutators in the cookbook now and it’s giving me ideas.

$mealplan->$end_date will not set the “end date” property in your $mealplan variable. It will set a property whose name is in the $end_date variable. For example, if you had previously done $end_date = 'something';, then $mealplan->$end_date = 1 would set a property called “something” to 1; it’s exactly equivalent to $mealplan->something = 1.

You’re getting the “Cannot set an empty field” error because you have not actually set $end_date to anything, so it’s equivalent to $mealplan-> = 1, which is not valid syntax.

What you actually want to be doing is something like $mealplan->end_date = date_create(), then in your template echo date_format($mealplan->end_date,"Y-m-d").

Hope that helps with understanding what’s going on for you.

Thanks, Zuluru

In my controller, I tried:
$end_date = $mealplan->end_date;
$mealplan->end_date = date_create();

and I got the error: Cannot set an empty field

So in my controller I tried instead:
$end_date = $mealplan->end_date;
$end_date = date_create($end_date);

but I got a whole slew of data returned that was declared: $dataForView =
In my template:
echo date_format($mealplan->end_date,"Y-m-d");

I just re-read your post and you wrote: $mealplan->$end_date will not set the “end date” property in your $mealplan variable. so I see now that I’m still doing it wrong.

In my controller, I tried:
$mealplan->end_date = date_create();
instead of $end_date = $mealplan->end_date; but it still gave me an error.

You probably want: -

$mealplan->end_date = date_create();
$end_date = $mealplan->end_date;

Assign the value of the object property before assigning it to another PHP variable. Having done so you want (I assume) to carry this value to your template, so after this you do: -


If you don't need to access the `$end_date` variable in the controller, you need not create a separate copy of the `$mealplan->end_date` object variable. Therefore this can be simplified to: -
$mealplan->end_date = date_create();
$this->set('end_date', $mealplan->end_date);

Now in your template you can display this variable as passed by the controller via: -

<?= $end_date />

Because what the set{} is doing is loading up the variables passed to it for the template - which is how you can use $end_date even though it need not exist in the controller as a variable. The compact is merely when the it does exist in the controller, and you want it to have the same variable name & value in the template - and you can pass an array so it really is nice & compact.

After reading your original question again, I struggle about the error message:

Are you working in “CalendarsController” or in “MealplansController”

I am working in CalendarsController

Thanks, Jawfin. I implemented the code that you provided and now my calendar displays the event date as today. It doeesn’t get the date from the database.

You had sample code setting the end date to create_date(), which is today’s date, so this is what we had understood that you wanted. Also, you’re using newEmptyEntity, which makes a brand new entity. If your $mealplan is in fact coming from the database with values already in it, then you don’t want to do any of those things. You just want to $this->set(compact('mealplan')); so that your $mealplan variable is available in your template, and then in the template use echo date_format($mealplan->end_date,"Y-m-d"); as you had at one point.

I guess the main issue in trying to help you here is that the real problem you’re trying to solve isn’t very clear; we’re sort of playing whack-a-mole with different symptoms that you report.

Yes, it wouldn’t get the date from a table as you didn’t query it. I also missed what dirk picked up that you’re looking for a value from another model in that controller.

So from your Calendar you need to find which meal plan record you want, then you can pass that. As we’re working with minimal information from you its hard to fill in the gaps - but I’ll give it a stab.
From this post I want foreach loop to execute only one time - #3 by makamo66 I know your Mealplans structure - is there even a schedule table? Anyway, here goes:-

$this->loadModel('Mealplans'); //import the desired table/model
$mealplan = $this->Mealplans
  ->where(['user_id=' => $THEIR_USER_ID, 
/* not sure where you have their id, from auth? like $this->Authentication->getResult()->getData()->id */
    'event_date=' => $THE_DATE_YOU_WANT_HERE])-> //is this syntax for dates ok?
/* that above code only returns the first meal plan of that date, there could be more than one? */
$this.set('end_date', $mealplan->end_date);
/*NOTE: I AM NOT CHECKING TO SEE IF THE QUERY IS EMPTY! so you'll need to do that */

Those variables in all caps you’ll need to provide - this code won’t work by just copy/paste.

Honestly though, this is kinda going above and beyond :slight_smile: