Proper Architecture
First, I would not do any business logic operations inside an entity. Especially not placing the code inside the entity. If you really think you must do it there, then call at least just a table method and encapsulate the logic there. An entity is thought to be a data object, nothing more. So doing save operations inside the entity inverts the architecture. This should be done inside the beforeSave() callback of the model.
Why is NOW() vs phps date() required?
Also, using PHP's functions to generate a date is not an option. It must be done on the MySQL server.
Why? I don't see a reason to not do so. Different timezone setting or something? Performance? NOW() is probably slower if you have lots of writes. Well, just make your phps default timezone match whatever the DB is using? You could then simply use the Timestamp behavior. I'm happy to learn something new about the implications of not using NOW() vs using date() but so far I see absolutely no need to use NOW().
Using NOW() with the CakePHP 3 ORM
If you would have read the manual you would have probably seen this:
CakePHP’s ORM offers abstraction for some commonly used SQL functions. Using the abstraction allows the ORM to select the platform specific implementation of the function you want. For example, concat is implemented differently in MySQL, PostgreSQL and SQL Server. Using the abstraction allows your code to be portable:
The documentation features an example as well:
// Results in SELECT COUNT(*) count FROM ...
$query = $articles->find();
$query->select(['count' => $query->func()->count('*')]);
Followed by a list of out of the box supported functions that includes:
now() Take either ‘time’ or ‘date’ as an argument allowing you to get either the current time, or current date.
If you keep on reading it will tell you
In addition to the above functions, the func() method can be used to create any generic SQL function such as year, date_format, convert, etc. For example:
I haven't had a need to use now() myself yet but based on the above information I guess it would be:
$date = $query->func()->now();
Then just save that value to the DB. I think the unit tests of the framework will show an example of how to use it as well.
Setting the date on create
Bonus Points: If I wanted to add a created_on field, how would I handle that with Entities? How could I set created_on only once when the record is first inserted, rather than on every update?
$entity->isNew() will allow you to check if the entity is new or not and act accordingly. If is new then set timestamp.
Bottom line
You're re-inventing the Timestamp behavior in the architecturally wrong place. It's up to you to do it right or not.
updated_onfield in your db asdateortimeordatetimeupdate_onwhenever an entity has been altered and you want to set thecreated_onwhen it's created? Take a look at: book.cakephp.org/3.0/en/orm/behaviors.html#using-behaviorsdatetime, but I want to set it's value to the result of the MySQL functionNOW().