Login | Register
My pages Projects Community openCollabNet

Discussions > dev > Propel event handler > Reply to message

propel
Reply to message

* = Required fields
* Subject
* Body
Attachments
Send reply to
Topic
Author (directly in email)
Please type the letters in the image above.

Original message

Author Ron Rademaker <r.rademaker@virtualbuilding.nl>
Full name Ron Rademaker <r.rademaker@virtualbuilding.nl>
Date 2007-11-15 02:45:32 PST
Message Hi all,

Last week I told you about my idea to create a backup function in propel
based on sql triggers. I've been working on this a bit this week only to
find out sql triggers and procedures have some very annoying properties.
This would result in having to redefine all triggers whenever you change
your schema, and often no errors if you forget to do this (and I *know*
I would something forget it and I would be *very* surprised if the other
developers here wouldn't). I don't think this is a good idea so I've
come up with a different, much more flexible, database independent
solution. The only drawback I see is that performance would be better if
the backup functionality was implemented in sql.

The basic idea is that you can add event to tables and columns (and
databases, though I haven't though of a useful example for that yet). An
example schema (I think the first event is obvious, I'll explain the
second later) could be:

<table name='foo'>
    <column name='id' primaryKey='true' type='INTEGER'
autoIncrement='true'/>
    <column name='date' type='TIMESTAMP'/>
    <column name='bar' type='VARCHAR'/>
    <event occasion='onChange'> <!-- this is now defined on the table,
but could also be defined on the bar column -->
       <handler name='setcolumn'>
          <parameter name='column' value='date'/>
          <parameter name='value' type='phpfunction'>
             <function name='date'>
                 <parameter value='"now"'/>
             </function>
          </parameter>
       </handler>
    </event>
    <event occasion='onCreate'>
       <handler name='saver' function='registerSave'/>
    </event>
    <event occasion='onSave'>
       <handler name='saver' function='triggerSave'/>
    </event>
</table>
<table name='bar'>
    <column name='id' primaryKey='true' type='INTEGER'
autoIncrement='true'/>
    <column name='bar' type='VARCHAR'/>
    <event occasion='onCreate'>
       <handler name='saver' function='registerSave'/>
    </event>
    <event occasion='onSave'>
       <handler name='saver' function='triggerSave'/>
    </event>
</table>

Both table foo and bar have the saver handler for the onCreate and
onSave event. I intent to implement the event / handler structure using
the observer pattern, Propel objects being Observables and event
handlers being Observers. The saver handler (observer) would be an
example where the onCreate event registers the propel object with the
handler and the onSave triggers save on all registered objects. This way
you can save multiple objects with a single save() call, potentially
this means you could tweak things so that you would add or update many
objects in a single sql query.
I know many of the things this will make possible are already possible
by overriding functions in the propel classes. I do believe however,
that this mechanism would prove to be very flexible and easier to
maintain than implementations in propel classes (I still have a whole
lot of classes where I need to change save(PDO $con = nul) to
save(PropelPDO $con = null)).
I'll be starting to build this shortly, anyone having suggestions,
improvements, seeing upfront bottlenecks, just let me know and if I
think it makes sense I'll try to do something useful with it ;)

Ron