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:
these fields are in your entity. you can access the fields in your controller like this:
$mealplan->event_date
$mealplan->end_date
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.
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.
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.
$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.
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.
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: -
$this->set(compact('end_date'));
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: -
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.
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
->find()
->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?
first();
/* 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