Log message #4204099

# At Username Text
# Sep 19th 2019, 06:12 conehead you have 1 request to show the existing data. And if posting data you have a second save
# Sep 19th 2019, 06:12 adam282 Yes, I do a `get()` but not in the same request. The `get()` is only executed when building the form. When the form is submitted, I want to update the entity (which the `id` is being passed in the routine as a `slug`) without doing another `get()` or `find()`
# Sep 19th 2019, 06:11 slackebot successfully')); } else { $this->Flash->error(__('Unable to create user')); } } ```
# Sep 19th 2019, 06:11 adam282 Here ``` if ($this->request->is('get')) { $user = $this->Users->get($id); $this->set(compact('user')); $this->render('Elements/update'); } if ($this->request->is('post')) { $user = $this->Users->patchEntity($user, $this->request->getData()); if ($this->Users->save($user)) { $this->Flash->success(__('User created
# Sep 19th 2019, 06:11 conehead if you want to edit an entity you need to show the existing data
# Sep 19th 2019, 06:10 admad guess i'll just shut up
# Sep 19th 2019, 06:10 adam282 Not true.
# Sep 19th 2019, 06:10 conehead even in cake 2
# Sep 19th 2019, 06:10 conehead you always need that
# Sep 19th 2019, 06:10 adam282 Which is added time and added resource usage.
# Sep 19th 2019, 06:10 adam282 But again, as I just pointed out, the CMS tutorial is doing a `findBySlug` before the update which is an added query.
# Sep 19th 2019, 06:10 admad "What is `preferred` the `3.x` way of doing this?" what the CMS tutorial shows
# Sep 19th 2019, 06:09 slackebot 'index']); } $this->Flash->error(__('Unable to update your article.')); } $this->set('article', $article); } ``` That’s an extra query
# 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)