Not all frameworks support true models and this is one thing that really sets the Solar Framework apart from a lot of frameworks.

So what sets Solar apart? Lets replicate what Zend Framework references on models in Solar.. You can find the ZF Docs Here

Taking a look through ZF docs it becomes quickly clear that in reality they do not have true model support, instead they walk you through how to create a model using ZF sql's setup.. Notice how you have to define setters, getters and various methods to get a "model" for your database.

Solar handles things a lot different.. Lets look at a Solar Model (while code assumes a general knowledge of Solar and skips all the generic setup that exists in any framework the idea of how powerful Solar models are should be clear)

class Jack_Model_Example extends Solar_Sql_Model {
   
    /**
     *
     * Model-specific setup.
     *
     * @return void
     *
     */

    protected function _setup()
    {

        $this->_model_name = 'example'; // How will I reference this model
        $this->_table_name = 'example'; // What table will this model access
        $this->_foreign_col = 'exp_id'; // How will I reference the primary id in other models
       
        /**
         * Define special column behavior.
         */

        $this->_sequence_cols['id'] = 'example__id';
// Lets solar that the id column should be sequenced.. This provides a way to do auto inc outside of sql if needed.

        /**
         * Define Validations
         */

        $this->_addFilter('name', 'validateNotBlank')
// Adds a filter, prevents saving and creating an example without a name

        /**
         * Define Relationships.
         */

        $this->_hasMany('comments'); // Lets the model know that it has a relationship with comments

    }

}

Thats it, the above code is everything you need to access the example table and it also provides a direct relation to any comments for each example..

So how do you use the above model?

$example_model = Solar::factory('Phpjack_Model_Example');

// Creating a new example
$example = $example_model->fetchNew();
$example->name = 'First example';
$example->desc = 'Long text for example';
$example->save(); // We have now created a new example, lets assume its id 1

// Getting an example
$example = $example_model->fetchById(1); // returns a single record for Id 1
echo $example->name;
echo $example->desc;

Thats it, the very basics needed to use Solar models.. if you like that you will love the fact that Solars models are relation aware. In the above model you have the folllowing line

   $this->_hasMany('comments');

What exactly does this line do? It tells Solar that Example can have many comments.. Solar will see this and pull the comments model and access comments as needed.. Taking the above example lets assume you have a couple comments related to Example 1..

<code>
$example_model = Solar::factory('Phpjack_Model_Example');

// Getting an example
$example = $example_model->fetchById(1); // returns a single record for Id 1
foreach($example->comments as $comment) {
    print_r($comment); // will print out all the columns of a comment
}

Whats that, I have access to the comments related to my Example and I didn't have to do any extra work? Yes.. when you accessed $example->comments solar loaded the comments model, used the reference in the Example model

        $this->_foreign_col = 'exp_id';

To see that it needed to find all comments with an exp_id of 1 and provides you clear and direct access to those comments.

This type of relationship linking can continue on no matter how deep your relationship goes. It is even possible to work with intermediate columns.. Like Member -> MembertoApp -> Applications.. Using Solar models you can access $member->applications

Thats it for today, If this quick taste has you wanting more please leave a comment letting me know and I will make sure to follow this up with some more detailed examples.

Comments

Isnt this what Zend_db_table_row is created for?