# |
Jul 25th 2017, 17:22 |
rogerpro |
Thanks. I need to be used to `dirty`indeed :) |
# |
Jul 25th 2017, 17:21 |
cleptric |
Scroll a bit down, there is a chart |
# |
Jul 25th 2017, 17:21 |
cleptric |
https://book.cakephp.org/3.0/en/orm/saving-data.html#saving-entities |
# |
Jul 25th 2017, 17:21 |
cleptric |
Yes, because your entity is not `new` and not `dirty` as you haven’t changed anything |
# |
Jul 25th 2017, 17:20 |
rogerpro |
* It does NOT execute `beforeSave`now: |
# |
Jul 25th 2017, 17:19 |
slackebot1 |
And I’m calling the table’s `save()`method. |
# |
Jul 25th 2017, 17:19 |
rogerpro |
Still trying to understand the complete workflow. It does executes `beforeSave`now: ``` public function resave($model = '') { $this->loadModel($model); $q = $this->$model->find()->where([ 'id' => '5c12a233-f0c6-449f-b493-28b4912b528e' ]); // just for testing with only 1 entity foreach ($q as $key => $value) { debug($key); $this->$model->save($value); |
# |
Jul 25th 2017, 17:16 |
cleptric |
I use grunt for less, but there is also a sass module for that |
# |
Jul 25th 2017, 17:15 |
inoas |
OT: anyone compiling sass strings via command line to css strings? |
# |
Jul 25th 2017, 17:04 |
rogerpro |
Nevertheless, I would like Cake to have an easy way to recall events on all entities, by design… Will think about it :thinking_face: |
# |
Jul 25th 2017, 17:03 |
rogerpro |
I will refactor it. |
# |
Jul 25th 2017, 17:03 |
rogerpro |
Anyway! let’s refactor it, this logic should just be called from `beforeSave` but not be coded inside it. I see it clear now. Better organized like that :slightly_smiling_face: |
# |
Jul 25th 2017, 17:01 |
rogerpro |
Let’s solve it… |
# |
Jul 25th 2017, 17:01 |
rogerpro |
Right! ,:) |
# |
Jul 25th 2017, 16:58 |
cleptric |
That’s it :slightly_smiling_face: |
# |
Jul 25th 2017, 16:58 |
cleptric |
Just setting sth in the entity mutates the object |
# |
Jul 25th 2017, 16:58 |
cleptric |
Because you don’t save it |
# |
Jul 25th 2017, 16:58 |
cleptric |
Why creating a new event instance if you just could call a method in your model :P |
# |
Jul 25th 2017, 16:58 |
rogerpro |
app table |
# |
Jul 25th 2017, 16:57 |
rogerpro |
(session is an Entity in this case) |
# |
Jul 25th 2017, 16:57 |
rogerpro |
it returns a Session object with a modified ‘duration’ field value. But it does not go to the DB table… |
# |
Jul 25th 2017, 16:57 |
rogerpro |
Right now I want to understand why this code on the `beforeSave`is executed but not reflected on the DB: `debug($session->set(‘duration’, $this->getInterval($begin, $end)));` |
# |
Jul 25th 2017, 16:56 |
rogerpro |
Yes, good idea, but why? Am I too much exigent to Cake? :) |
# |
Jul 25th 2017, 16:54 |
cleptric |
What about extracting the logic inside your `beforeSave` into a method and call it in you shell and beforeSave? |
# |
Jul 25th 2017, 16:53 |
rogerpro |
This is on a shell. |
# |
Jul 25th 2017, 16:52 |
slackebot1 |
$this->$model->find()->where([ ‘id’ => ‘5c12a233-f0c6-449f-b493-28b4912b528e’ ]); // just for testing with only 1 entity $my_event = new \Cake\Event\Event(‘Model.beforeSave’); foreach ($q as $key => $value) { $this->$model->beforeSave($my_event, $value); } } ``` |
# |
Jul 25th 2017, 16:52 |
rogerpro |
I understand, thanks. So now I’m running entities in a foreach loop and the beforeSave is executed, but not actually saved the value. ``` /** * Resave all entities of a model. * * This will trigger many events (i.e. beforeSave) so data will * be consistent with the current implementation of the Entity. */ public function resave($model = ‘’) { $this->loadModel($model); $q = |
# |
Jul 25th 2017, 16:31 |
dereuromark |
you cannot, by design, for atomatic operations, just dont use them if you need the callbacks |
# |
Jul 25th 2017, 16:19 |
rogerpro |
Maybe will be good to enable after and beforeSave on `updateAll` upon request as a feature. |
# |
Jul 25th 2017, 16:17 |
rogerpro |
I can just refactor the logic of the `beforeSave` and call it from a Shell and from the `beforeSave` itself. But I just wanted to know how to do it _comme il faut_ |
# |
Jul 25th 2017, 16:16 |
rogerpro |
For efficiency, @admad. Why using a behaviour many times per minute if I only want it once a year? |
# |
Jul 25th 2017, 16:15 |
rogerpro |
And, by the way, `updateAll`does not trigger `beforeSave` https://book.cakephp.org/3.0/en/orm/saving-data.html#bulk-updates |
# |
Jul 25th 2017, 16:14 |
admad |
Why not if it does the job you want? |
# |
Jul 25th 2017, 16:13 |
rogerpro |
About CounterCacheBehavior: I don’t want to enable a Behaviour on a Model just to use it once. I will study it more deeply |
# |
Jul 25th 2017, 16:12 |
rogerpro |
Yes, but this is not the case. I want tu run this “recalculation process” just now and maybe for some particular reasons on the future. Not on any regular basis. |
# |
Jul 25th 2017, 16:11 |
cleptric |
Normally you would call `updateAll` in your afterSave if you want to update something based on a saved entity |
# |
Jul 25th 2017, 16:11 |
rogerpro |
Thanks! I will take a look. It was mentioned many times at CakeFest |
# |
Jul 25th 2017, 16:10 |
rogerpro |
Maybe with an Expression on the query() but… I find this complicated |
# |
Jul 25th 2017, 16:10 |
cleptric |
It does similar things |
# |
Jul 25th 2017, 16:10 |
cleptric |
Have a look at the CounterCacheBehavior |
# |
Jul 25th 2017, 16:10 |
rogerpro |
The goal is to trigger a save on all entities. `afterSave`will work for only one specific entity |