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.
