Ticket #378 (closed task: fixed)
Add solution for default expressions
| Reported by: | hans | Owned by: | hans |
|---|---|---|---|
| Priority: | high | Milestone: | 1.3 |
| Component: | Generator | Version: | 1.3.0beta2 |
| Severity: | normal | Keywords: | default values expressions |
| Cc: |
Description
This is based on email proposals by Alan on-list.
- Schema.xml <column> has both a @defaultSqlExpr and a @defaultPhpExpr attribute.
- Schema.xml <table> also has a boolean @reloadOnInsert attribute (which can be over-ridden at runtime to improve performance when batch inserting and you don't need the "correct" object after insertion). This should be trivial now that reload() method was added in [606].
The reloadOnInsert would also allow the propel model to have the correct information for those using insert triggers. And for those where performance is important and but the slight differences in values between the SQL and PHP version of expression (think insertion timestamp) don't matter.
- (Maybe) Schema.xml <column> also has a boolean @preferSqlExpr which tells the system whether or not to use the SQL expression if there is *also* a PHP expression.
For instance, a creation_dts would have
<column name="creation_dts" defaultSqlExpr="NOW()" defaultPhpExpr="time()" preferSqlExpr="false" />
When you instantiate the object, the PHP instance's creation_dts property would be set to time(), and at "insert" time, the actual php value would be used (instead of inserting with a NULL creation_dts).
If @preferSQLExpr=true, then at insert time, NULL would be submitted (on new objects only) and the php expression would stay with its existing original time() value.
This approach probably will necessitate an object initialization phase (for objects with default values). Fort this we can create a new applyDefaultValues() method and call it from __construct(). Obviously, this means that developers that are currently creating a __construct() method in their stub classes will need to now call parent::__construct() in order to have objects initialized properly. It's not perfect, but it's probably the best solution.
Change History
comment:2 Changed 3 years ago by matt@…
I have a feature request relating to this. Can we also have a boolean @reloadOnUpdate attribute as, for example, timestamps can change on update as well as insert.
comment:3 Changed 3 years ago by hans
In response to David's suggestion, I think that for 1.3 release it would be simpler to stick with attributes. There are only 3 attributes total for column:
- defaultSqlExpr
- defaultPhpExpr
- preferSqlExpr
Before we separate these out into a sub-element, we should carefully consider the schema on the whole, as there will be other things that probably should change too. I see this more drastic change as a 2.0 change.
comment:6 Changed 3 years ago by hans
I added an applyDefaultValues() method, called from constructor, in changeset:619. The other aspects of this ticket have not yet been addressed, however.
comment:7 Changed 3 years ago by hans
- Priority changed from normal to high
- Milestone changed from 1.3 to 2.0
Another thing to consider ... should the PHP defaults be evaluated in the constructor or deferred until save() ... ? I can imagine cases (albeit rare ones) where there would be a difference there.
I'm going to move this change ahead to 2.0. We have partial support now for the defaultExpr, but this prefer Sql vs. Php is something that should probably be thought-out a bit more.
comment:8 Changed 3 years ago by hans
- Status changed from assigned to closed
- Resolution set to fixed
I broke the @reloadOnInsert and @reloadOnUpdate out into their own ticket. See ticket:555. I'm going to close this now, as further work really should be a new ticket. Yes, we have not followed the implementation in this ticket, but we have addressed the basic need (for server-set defaults, triggers) by the changes that have been made in [606] and the follow-on work that will happen for ticket:555.
comment:9 Changed 3 years ago by hans
- Status changed from closed to reopened
- Resolution fixed deleted
comment:10 Changed 3 years ago by hans
- Status changed from reopened to closed
- Resolution set to fixed
- Milestone changed from 2.0 to 1.3
comment:11 Changed 3 years ago by hans
- Summary changed from Add new php/sql expression default solution proposed by Alan to Add solution for default expressions
David asks:
Can we please have a <default> child tag for columns, that would be much cleaner than a million attributes :)