# |
Sep 19th 2019, 06:09 |
adam282 |
Yeah, see this has it doing a select before the update: ``` public function edit($slug) { $article = $this->Articles->findBySlug($slug)->firstOrFail(); if ($this->request->is(['post', 'put'])) { $this->Articles->patchEntity($article, $this->request->getData()); if ($this->Articles->save($article)) { $this->Flash->success(__('Your article has been updated.')); return $this->redirect(['action' => |
# |
Sep 19th 2019, 06:09 |
conehead |
So using 'patchEntity' should be the way to go whenever you get data from forms or where users input data |
# |
Sep 19th 2019, 06:09 |
adam282 |
Because like I said, documentation versus tutorials show different ways |
# |
Sep 19th 2019, 06:08 |
conehead |
Usually you want to validate |
# |
Sep 19th 2019, 06:08 |
conehead |
Both are valid ways |
# |
Sep 19th 2019, 06:08 |
adam282 |
Okay, thank you. That’s the answer I was looking for. What is `preferred` the `3.x` way of doing this? |
# |
Sep 19th 2019, 06:08 |
conehead |
Setting variables directly will not |
# |
Sep 19th 2019, 06:08 |
conehead |
PatchEntity will trigger validation |
# |
Sep 19th 2019, 06:08 |
conehead |
if you wish you still can do `$user->id = 3` |
# |
Sep 19th 2019, 06:07 |
conehead |
not really |
# |
Sep 19th 2019, 06:07 |
adam282 |
Two lines of code. CakePHP 3.x want’s like 4 or more to do the same thing |
# |
Sep 19th 2019, 06:07 |
adam282 |
CakePHP 2.x was much easier. Set the ID, run a save with the new data and you’re done. |
# |
Sep 19th 2019, 06:06 |
adam282 |
Not making excuses, making observations that there seems to be multiple ways to skin the cat here. |
# |
Sep 19th 2019, 06:06 |
adam282 |
For example, the documentation shows: ``` $article = $articlesTable->newEntity(); $article->title = 'An article by mark'; $article->author = $author; if ($articlesTable->save($article)) { // The foreign key value was set automatically. echo $article->author_id; } ``` Where the Blog tutorial shows: ``` $user = $this->Users->newEntity(); $user = $this->Users->patchEntity($user, $this->request->getData()); $this->Users->save($user); ``` |
# |
Sep 19th 2019, 06:05 |
admad |
hey if you wanna make excuses for not doing it then good luck |
# |
Sep 19th 2019, 06:05 |
adam282 |
The problem with those tutorials is from the Blog, to the CMS to the actual documentation, everything is explained different ways |
# |
Sep 19th 2019, 06:04 |
adam282 |
Ugh |
# |
Sep 19th 2019, 06:04 |
hmic |
the entity you patch comes from a datastore and has the information! |
# |
Sep 19th 2019, 06:04 |
admad |
you also should do the CMS tutorial of 3.x :slightly_smiling_face: |
# |
Sep 19th 2019, 06:04 |
adam282 |
Well no, that won’t work either |
# |
Sep 19th 2019, 06:04 |
adam282 |
Suppose I could `$user->id = $id` |
# |
Sep 19th 2019, 06:04 |
admad |
no it wont |
# |
Sep 19th 2019, 06:03 |
admad |
the `$user` entity passed as 1st argument already has the id |
# |
Sep 19th 2019, 06:03 |
adam282 |
So the form I submit would need the `id` as a hidden field? I don’t like that. |
# |
Sep 19th 2019, 06:03 |
hmic |
adam282: zou provide it with an entity to patch, this has all the information needed! |
# |
Sep 19th 2019, 06:02 |
adam282 |
How does `$user = $this->Users->patchEntity($user, $this->request->getData());` know which row to update? In CakePHP 2.x I was able to do `$this->User->id = $id;` and then `$this->User->save($this->request->data)` and it would update the row based on the `id` set. How do I do this in CakePHP 3.8? |
# |
Sep 19th 2019, 05:38 |
adam282 |
Please understand, the link you’re referencing that is already created is not coming from the query. They are creating it in the view (.ctp) file. |
# |
Sep 19th 2019, 05:37 |
Fastidius |
cool cool, will do...i did plan on doing it but so far have been able to figure most of their glitches out with a little prompting...practical time i guess, CHEERS for your help 8) |
# |
Sep 19th 2019, 05:35 |
admad |
Fastidius: i suggest you first do the blog / cms tutorial for the cake version your app is using to understand the basics |
# |
Sep 19th 2019, 05:33 |
adam282 |
Why they gotta change shit that was working? LoL |
# |
Sep 19th 2019, 05:33 |
admad |
@adam282 make sure you are using correct placeholders http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details |
# |
Sep 19th 2019, 05:32 |
Fastidius |
mostly got my head around it but the source of the data is a bit vague |
# |
Sep 19th 2019, 05:32 |
adam282 |
Your controller function is loading a template file which is being parsed and returned as the view. In that view/template will be what you’re looking for unless whoever wrote the app decided to put HTML in the controller before they returned the data from the database query, which is not intelligent. |
# |
Sep 19th 2019, 05:31 |
adam282 |
Have you looked at the view for that page? Do you understand how CakePHP is put together with Controllers, Models, Views, Entity, etc? |
# |
Sep 19th 2019, 05:31 |
Fastidius |
fair enough but i have a similar page with the links so in theory its there but I cant even see where its selecting fields to see what options i can have |
# |
Sep 19th 2019, 05:29 |
adam282 |
You would need to make it a link within the view |
# |
Sep 19th 2019, 05:29 |
adam282 |
SQL queries do not return links unless that’s what is in the database table column. |
# |
Sep 19th 2019, 05:29 |
Fastidius |
basically the first field it returns is a string but i want it to be a link....i have the same thing on another page its just a UI inconsitency but i cant locate the bits in controlers |
# |
Sep 19th 2019, 05:28 |
adam282 |
Ah, so in this case `paginate` is executing the query |
# |
Sep 19th 2019, 05:28 |
Fastidius |
thats the index i want to change |
# |
Sep 19th 2019, 05:28 |
Fastidius |
https://pastebin.com/9qPZ9Pwf |