# |
Jun 22nd 2021, 06:20 |
angelxmoreno |
tbh i never set it in the 21 years i’ve been coding even back in the html4 days. my audience and language has always been the US and english..boring i know |
# |
Jun 22nd 2021, 06:18 |
angelxmoreno |
@conehead why it matters --> https://www.w3.org/International/questions/qa-html-language-declarations#langvalues what “tools” ? would you like to see? |
# |
Jun 22nd 2021, 05:32 |
conehead |
Is there actually a need/advantage to change "<html lang="en">" to the correct language? Does ist affect the page in many ways? Was just wondering cause Cake does not seem to have any tools for this? |
# |
Jun 22nd 2021, 05:32 |
conehead |
Good morning everyone! |
# |
Jun 21st 2021, 19:22 |
kevin.pfeifer |
@vinicius.big I would create a global `var MY_I18N` variable an populate it with the translations you need |
# |
Jun 21st 2021, 19:19 |
kevin.pfeifer |
what :point_up: said :) I though you had other variables outside the anonymous function which you need but accessing variables inside the class/object (you are in a Table object space here) can be done via $this without passing the via the `use` |
# |
Jun 21st 2021, 19:19 |
vinicius.big |
hey folks! What is your suggestion to translate JS strings on Cake? There is a lot of JS libraries for this but, I would like to see your experience on it |
# |
Jun 21st 2021, 19:12 |
greg138 |
As for what do do in the function, do everything that needs to be "all or nothing" saved. |
# |
Jun 21st 2021, 19:11 |
greg138 |
You should be able to access `$this` in that function without any issue, so need real need to pass in the model either (you can use `$this->Tags`). |
# |
Jun 21st 2021, 19:09 |
alamnaryab |
yes that was the issue even without return false it worked now next step @kevin.pfeifer Should I pass $this->request->getData() to this anonymous function or make separate function for each model e.g next I have to save record in notification table saying "Logged-in-user has saved new tag at id = newly-saved-Tag->id" |
# |
Jun 21st 2021, 18:59 |
greg138 |
Any chance you're using a database that just doesn't support rollback? The MyISAM engine in MySQL, for example, doesn't. |
# |
Jun 21st 2021, 18:48 |
alamnaryab |
still redirected to index and record inserted ``` $entity = $this->Tags->patchEntity($tag, $this->request->getData()); $model = $this->Tags; $this->Tags->getConnection()->transactional(function () use ($model, $entity) { $model->save($entity, ['atomic' => false]); return false; }); return $this->redirect(['action' => 'index']);``` |
# |
Jun 21st 2021, 18:46 |
greg138 |
Try returning `false` from inside your "transactional" function. |
# |
Jun 21st 2021, 18:45 |
alamnaryab |
I tried below code, I have not written `commit` still it is saved in table ``` $entity = $this->Tags->patchEntity($tag, $this->request->getData()); $model = $this->Tags; $this->Tags->getConnection()->transactional(function () use ($model, $entity) { $model->save($entity, ['atomic' => false]); }); return $this->redirect(['action' => 'index']);``` |
# |
Jun 21st 2021, 18:39 |
greg138 |
Doesn't the save code check that it's not already in a transaction before starting a transaction? |
# |
Jun 21st 2021, 18:21 |
kevin.pfeifer |
https://stackoverflow.com/questions/41896991/why-is-the-automatic-transaction-option-in-cakephp-is-called-atomic/41898090 |
# |
Jun 21st 2021, 18:21 |
kevin.pfeifer |
```->save($entity, ['atomic' => false]);``` |
# |
Jun 21st 2021, 18:21 |
kevin.pfeifer |
is the term that says that it shouldn't have a transaction |
# |
Jun 21st 2021, 18:21 |
kevin.pfeifer |
```'atomic' => false``` |
# |
Jun 21st 2021, 18:18 |
alamnaryab |
I got this `use` but I am not getting how can I convert my code to achieve transactions as you also said `->save()` already has its own `commit` |
# |
Jun 21st 2021, 18:10 |
kevin.pfeifer |
so everything you expect inside your function needs to be passed via the `use` at the end of the first line, otherwise you don't have access to these variables |
# |
Jun 21st 2021, 18:09 |
kevin.pfeifer |
basically ```$result = 0; $one = function() { var_dump($result); }; $two = function() use ($result) { var_dump($result); }; $three = function() use (and$result) { var_dump($result); }; $result++; $one(); // outputs NULL: $result is not in scope $two(); // outputs int(0): $result was copied $three(); // outputs int(1)``` |
# |
Jun 21st 2021, 18:07 |
kevin.pfeifer |
they are called php anonymous functions https://www.php.net/manual/en/functions.anonymous.php |
# |
Jun 21st 2021, 18:07 |
alamnaryab |
in your second last message you said "need to pass inside via `use`" |
# |
Jun 21st 2021, 18:07 |
kevin.pfeifer |
yes, thats why you would have to wrap all those ->saveOrFail() inside the ->transactional(); |
# |
Jun 21st 2021, 18:04 |
alamnaryab |
`->saveOrFail()` will also revert that entity only not all those which saved successfully before it in same process |
# |
Jun 21st 2021, 18:01 |
kevin.pfeifer |
instead of the normal ->save() function which only returns the $entity with the errors |
# |
Jun 21st 2021, 18:00 |
kevin.pfeifer |
there is also a `->saveOrFail($entity)` function which throws an exception if something couldn't be saved. |
# |
Jun 21st 2021, 17:50 |
kevin.pfeifer |
well everything from "outside" the transactional function you need to pass inside via the `use` at the end Other then that it should be pretty much the same from your current logic |
# |
Jun 21st 2021, 17:47 |
alamnaryab |
But I think I can not use `->transactional()` also because all these entities are not for same model, also each entity properties has foreign keys from previously saved record. and there are some conditional properties. |
# |
Jun 21st 2021, 17:36 |
kevin.pfeifer |
it seems that usually every `->save($entity)` call has its own transaction, thats why you can't rollback them now |
# |
Jun 21st 2021, 17:35 |
slackebot1 |
docs for cakephp 4 https://book.cakephp.org/4/en/orm/saving-data.html#converting-multiple-records But this `->transactional()` function is still present in cake 4 so you could try that I guess :man-shrugging: |
# |
Jun 21st 2021, 17:35 |
kevin.pfeifer |
looking at https://book.cakephp.org/3/en/orm/saving-data.html#converting-multiple-records there is a `->transactional()` function you could use to wrap all those saves in one transaction ```$this->Model1->getConnection()->transactional(function () use ($articles, $entities) { foreach ($entities as $entity) { $articles->save($entity, ['atomic' => false]); } });``` But this part has been removed from the |
# |
Jun 21st 2021, 17:23 |
slackebot1 |
Model2'); } if(!$this->Model9->save(...) ){ throw exception('Could not save Model9'); } $datasource->commit(); }catch(Exception $ex){ $datasource->rollback(); $this->Flash->error($ex->getMessage(). ' Line:'.$ex->getLine()); }``` |
# |
Jun 21st 2021, 17:23 |
alamnaryab |
in cakephp2 I am having something like this ```$datasource = $this->Model1->getDataSource(); $datasource->begin(); try{ if(!$this->Model1->save(...) ){ throw exception('Could not save Model1'); } if(!$this->Model2->save(...) ){ throw exception('Could not save Model2'); } $this->Model3->id = $this->Model2->id; /////insert statement if(!$this->Model3->save(...) ){ throw exception('Could not save |
# |
Jun 21st 2021, 17:20 |
kevin.pfeifer |
and you get false back from `$this->Tags->save()` if it didn't save |
# |
Jun 21st 2021, 17:19 |
kevin.pfeifer |
well `$this->Tags->save()` will not save if there are errors in the entity and therefore you don't have to manually rollback. You have multiple events you can hook into in the `src/Table/MyTable.php` https://book.cakephp.org/3/en/orm/table-objects.html#lifecycle-callbacks But maybe I don't understand your usecase. |
# |
Jun 21st 2021, 17:16 |
slackebot1 |
error message to user. it will be difficult to convert all that logic to sql queries. |
# |
Jun 21st 2021, 17:16 |
alamnaryab |
https://cakesf.slack.com/archives/C053DPNGT/p1624270771154400 @kevin.pfeifer about transcation topic at above link I have cakephp2 project to convert to cakephp4 by re-writing all there I have used transactions with models not custom sql queries for more detail I have dynamic workflow on each step I have to insert in some table, while make update in some tables at last send eamil if any thing failed I have to revert and show |
# |
Jun 21st 2021, 16:29 |
ndm |
If by forwarding you mean you're just redirecting a user, then it should pick up the session where it left, given that the session cookie is still valid. |
# |
Jun 21st 2021, 16:28 |
ndm |
Yes, very likely that it doesn't send the session cookie. Depending on what that external thing is, it might not even have that cookie. |