[Solar-talk] Solar_Sql_Model to DataMapper/Record/Collection

Rodrigo Moraes rodrigo.moraes at gmail.com
Sat Jul 21 20:01:37 CDT 2007


Hi,
Following some conversations off-list, I converted Solar_Sql_Model to
a DataMapper - Record - Collection group of classes. Although they
need review and refactorings and additions, I've just got everything
working, from table creation to loading related records and executing
insert/update/delete's.

There's a lot of things to do, but it's quite advanced. I'd love some
help with the code, so let me know if you test it and find bugs or see
improvements. We need to implement some missing parts too. Here are
the classes:

    http://dev.tipos.org/browser/trunk/Tipos/Orm

It's pretty much Solar_Sql_Model after 5 hours in a blender. :) Code
was moved here and there to achieve what some of us believe is a
better approach to models. Excuse the names, I've added it to a /Orm
directory to not mix them with my current models.

* Tipos_Orm_Model - data mapper: stores info about the table, model
relationships, filters, and executes database operations.
* Tipos_Orm_Record - represents a single record; extends Solar_Struct;
I tried to follow the previous Solar record implementation and added
Solar_Sql_Model new stuff. Each model can implement its own record
class.
* Tipos_Orm_Collection - represents a collection of records; extends
Tipos_Orm_Record.  Each model can implement its own collection class.
* Tipos_Orm_Catalog - Solar_Sql_Model_Catalog with almost no changes.
* Tipos_Orm_DataFilter - Solar_Sql_Model_Datafilter with no changes
(the filter part of this model is still missing and wasn't tested).

Below are some code examples (pretty obvious I'm afraid). :)

-- rodrigo

--------------------------------
Examples

    // Fetching a new record (creates the table if not yet created)
    $model = Solar::factory('Tipos_Model_Area');
    $record = $model->fetchNew();

    // Saving some data
    $record->name = 'My record name';
    $record->save();

    // Updating some data
    $record->name = 'I changed my mind';
    $record->save();

    // Deleting the record
    $record->delete();

The model is always available for a record/collection through getModel():

    // Get the model and execute a new fetch
    $model = $record->getModel();
    $collection = $model->fecthAll();

    // Update collection
    foreach($collection as $another_record) {
        $another_record->status = 1;
    }
    $collection->save();


More information about the Solar-talk mailing list