3.8. Special Column Processing

You can tell the model to apply special processing to certain columns when records are fetched and saved.

[Note] Note

Auto-increment columns are recognized by the model automatically when it examines the database, and so don't need special identification.

3.8.1. Created Timestamp

The $_created_col property notes a table column to be automatically populated with the timestamp when the row is first inserted to the database. By default, the column is called created, but you can call it anything you like. If the column is not present, or if $_created_col is empty, no special processing occurs.

<?php
class Vendor_Model_Foo extends Vendor_Sql_Model
{
    public function _setup()
    {
        // use a column called "first_added" as the created timestamp column
        $this->_created_col = 'first_added';
    }
}

3.8.2. Updated Timestamp

The $_updated_col property notes a table column to be automatically populated with the timestamp when the row is updated at the database. By default, the column is called updated, but you can call it anything you like. If the column is not present, or if $_updated_col is empty, no special processing occurs.

<?php
class Vendor_Model_Foo extends Vendor_Sql_Model
{
    public function _setup()
    {
        // use a column called "last_changed" as the updated timestamp column
        $this->_updated_col = 'last_changed';
    }
}

3.8.3. Auto-Sequence

The $_sequence_cols property notes one ore more table column to be automatically populated with the value of a sequence when the column is null. The key is the column name, and the value is the sequence name. If only a value is present, it is used as both the column name *and* the seqence name.

<?php
class Vendor_Model_Foo extends Vendor_Sql_Model
{
    public function _setup()
    {
        // when "bar" is empty, popualate it with the next value
        // from the sequence "zim"
        $this->_sequence_cols['bar'] = 'zim';
        
        // when "dib" us empty, populate it with the next value
        // from the sequence also called "dib"
        $this->_sequence_cols[] = 'dib';
    }
}

3.8.4. PHP Serialize

The $_serialize_cols property identifies columns that should be serialize()d when saved to the database, and unserialize()d when they are retrieved. This allows you to seamlessly use an array as a record property.

<?php
class Vendor_Model_Foo extends Vendor_Sql_Model
{
    public function _setup()
    {
        // the column 'user_prefs' should be serialized on save,
        // and unserialized on retrieval
        $this->_serialize_cols[] = 'user_prefs';
    }
}

3.8.5. XML Structs

The $_xmlstruct_cols property identifies columns that should be converted to Solar_Struct_Xml objects when retrieved from the database, and back to XML when saved to the database. This allows you to seamlessly work with an XML-struct object as a record properties.

<?php
class Vendor_Model_Foo extends Vendor_Sql_Model
{
    public function _setup()
    {
        // the column 'xmldata' should be converted to a Solar_Struct_Xml
        // object on retrieval, and back to XML on save
        $this->_xmlstruct_cols[] = 'xmldata';
    }
}


Local