How to formulate that sql command in Cakephp 3.4x

UPDATE membres SET age = YEAR (CURDATE()) - YEAR (date_de_naissance);

Is this something you are going to run on a daily basis? Why not skip that and use an accessor function to calculate it in PHP as required?

What is accessor function and where to incorporate it in my app…?
It is run on a daily basis

You can use an accessor function to create a virtual field for the age. You’d then just reference it like $member->age, same as today to access the database column, but it would do the calculation on the fly, eliminating the need to run this daily process.

In short, you’d eliminate the database column and the daily process, add one function to the entity, and everything else (views, etc.) would likely be unchanged. (If you have something where you’re sorting by age, you can just as easily sort by birthdate instead.)

age is a field defined in database as int(3); We want this field to be updated everyday and every time an operator correct or ajust date of Birth. So now, we use PhpMyAdmin and we do that SQL command;
So my question is how to add that command somewhere in an index.php, controller or model ?? Table name is «members»; Author of the app is not reachable anymore and we are just néophytes to CakePHP; How to do the same as: UPDATE membres SET age = YEAR (CURDATE()) - YEAR ( date_de_naissance ); in CakePHP code??

something like this, just adjust it for your needs

       $q = $this->Notifications

                'age' => $q->newExpr('YEAR(NOW()) - YEAR(created_at)')
            ], []);
1 Like

Getting error: Error: [Cake\View\Exception\MissingHelperException] Helper class NotificationsHelper could not be found.

You are presumably trying to do this in a view. That’s very much the wrong place. Also, presumably, your model would be something like Members instead of Notifications.

If you’re trying to learn Cake, I’d suggest going through the tutorial to learn about what the various pieces are and how they come together. If you’re not interested in learning Cake and just need some adjustments made to your application, might I suggest hiring someone that knows Cake to do it for you.

Thanks for the advice Zuluru,

Thank so much Graziel, it works! I included the following statement in: MembresController.php
My statement is:
‘age’ => $q->newExpr(‘YEAR(CURDATE()) - YEAR(date_de_naissance)’)
], []);