DooPHP IRC channel


Multiple Extension for Model

Discussion about new desired features.

Re: Multiple Extension for Model

Postby wendy » Thu Aug 06, 2009 6:52 pm

What do you guys think about this model mapper configuration format? The rest will be taken care of by the model mapper class, to generate the table metadata and relationship with one another. The model will not generate any files, instead it will generate a model configuration files, something like cache. This part was done already, the only one left is the fetch, insert, update, and delete, which still covers 60% of this whole model. Hehee...
Code: Select all
<?php
/**
 * Sample Model Mapping and MetaData Configuration
 */

/**
 * Model relationship mapping.
 *
 * Define all available relationship between two models, which might comes from database or other model engine supported
 * by this framework. All models metadata involves in this relationship mapping will be parsed automatically from
 * database.
 *
 * Sample Configuration:
 *
 * $map['RELATION_ID'] = array(
 *     DooModelMapper::TYPE         => DooModelMapper::ONE_TO_MANY,                                       // constant
 *     DooModelMapper::LEFT_MODEL   => 'PRIMARY_MODEL_CLASS_NAME',                                          // string
 *     DooModelMapper::LEFT_COLUMN  => array('PRIMARY_COLUMN_NAME_1', 'PRIMARY_COLUMN_NAME_2'),     // string or array
 *     DooModelMapper::RIGHT_MODEL  => 'SECONDARY_MODEL_CLASS_NAME',                                        // string
 *     DooModelMapper::RIGHT_COLUMN => array('SECONDARY_COLUMN_NAME_1', 'SECONDARY_COLUMN_NAME_2'), // string or array
 * );
 *
 * There are only 2 types of relationship:
 * - One To One (1-1)  : DooModelMapper::ONE_TO_ONE
 * - One To Many (1-N) : DooModelMapper::ONE_TO_MANY
 *
 * Left and right model/column will follow the relationship defined above, so the left will always be the primary model
 * and column key, and the right will always be the secondary (foreign/joined) model and column key.
 */

$map['PostCategory'] = array(
   DooModelMapper::RELATION     => DooModelMapper::ONE_TO_MANY,
   DooModelMapper::LEFT_MODEL   => 'Post',
   DooModelMapper::LEFT_COLUMN  => array('id'),
   DooModelMapper::RIGHT_MODEL  => 'RelPostCategory',
   DooModelMapper::RIGHT_COLUMN => array('post_id')
);

$map['CategoryPost'] = array(
   DooModelMapper::RELATION     => DooModelMapper::ONE_TO_MANY,
   DooModelMapper::LEFT_MODEL   => 'Category',
   DooModelMapper::LEFT_COLUMN  => array('id'),
   DooModelMapper::RIGHT_MODEL  => 'RelPostCategory',
   DooModelMapper::RIGHT_COLUMN => array('category_id')
);

$map['PostTags'] = array(
   DooModelMapper::RELATION     => DooModelMapper::ONE_TO_MANY,
   DooModelMapper::LEFT_MODEL   => 'Post',
   DooModelMapper::LEFT_COLUMN  => array('id'),
   DooModelMapper::RIGHT_MODEL  => 'Tags',
   DooModelMapper::RIGHT_COLUMN => array('post_id')
);

$map['UserPost'] = array(
   DooModelMapper::RELATION     =>DooModelMapper::ONE_TO_MANY,
   DooModelMapper::LEFT_MODEL   => 'User',
   DooModelMapper::LEFT_COLUMN  => array('id'),
   DooModelMapper::RIGHT_MODEL  => 'Post',
   DooModelMapper::RIGHT_COLUMN => array('user_id')
);

$map['UserAttribute'] = array(
   DooModelMapper::RELATION     => DooModelMapper::ONE_TO_ONE,
   DooModelMapper::LEFT_MODEL   => 'User',
   DooModelMapper::LEFT_COLUMN  => array('id'),
   DooModelMapper::RIGHT_MODEL  => 'UserAttribute',
   DooModelMapper::RIGHT_COLUMN => array('user_id')
);

$map[DooModelMapper::INDEPENDENT] = array(
   'Log'
);


Here is the way you fetch the data, with no limit on the level of recursion you can go through. I don't have the pretty image diagram with me about the concept of multi level fetching, but I believe Leng has a better idea on what I'm trying to say through this.
Code: Select all
   /**
    * SAMPLE RELATION EXTENSION
    *
    * // This is to be used as an extension parameters on all sql megic method below.
    * // NOTE: All usage of tables and columns of models with sql magic method, whether it's for from, where,
    * //       or order will need to be extended first.
    * $extension = array(                // Level 1 (only one group)
    *     'RELATION_ID_1',               // Level 1 (only one group)
    *     'RELATION_ID_2',               // Level 1 (only one group)
    *     'RELATION_ID_3' => array(      // Level 2 (1st group)
    *         'RELATION_ID_4',           // Level 2 (1st group)
    *         'RELATION_ID_5' => array(  // Level 3 (2nd group)
    *             'RELATION_ID_6'        // Level 3 (2nd group)
    *         ),
    *         'RELATION_ID_7' => array() // Level 3 (3rd group)
    *      ),
    *     'RELATION_ID_8' => array(      // Level 2 (4th group)
    *         'RELATION_ID_9'            // Level 2 (4th group)
    *     )
    * );
    */
   
   /**
    * Universal fetch model data method.
    *
    * SAMPLE OPTION USAGE
    *
    * $option = array(
    *     'from' => '*',
    *     'where' => '',
    *     'order' => array()
    * );
    *
    * SAMPLE OPTION PARAMETERS
    *
    * // All model is being referenced through their class name and column name through their native sql name.
    * $option['from'] = NULL;  // Use NULL ot fetch no columns from all fetched models.
    * $option['from'] = '*';   // Use a string "*" to fetch all available columns from all fetched models.
    * $option['from'] = array( // Use array if there are selected columns to be fetched from main model.
    *     'MainModelClassName' => '*',   // Fetch all columns from MainModelClassName
    *     'RELATION_ID_2' => array(      // Fetch selected columns from ModelCLassName2
    *         'column_name_1',
    *         'alias' => 'column_name_2' // Use alias for returned column, has to be unique.
    *     ),
    *     'RELATION_ID_3' => NULL,       // Fetch no column, might as well don't include it here.
    *     'alias' => 'AVG({ModelClassName3}.column_name_3)' // Use expression and alias, it has to be unique.
    * );
    *
    * // All model name need to be enclosed with curly bracket "{}" and use "MAIN" for defining main model.
    * // Semi colon ":" followed by parameter name can be used as a placeholder for parameters with associated name.
    * // Question mark "?" can be used as a no name placeholder for parameters with no associated name. It will be
    * //   used by matching the position encountered in the SQL statement.
    * $where = "{ModelClassName1}.column_name_1 = :param1 OR ({MAIN}.column_name_2 = ? AND {ModelClassName2} > ?)";
    *
    * // Manual order sql construction.
    * // All model name need to be enclosed with curly bracket "{}" and use "MAIN" for defining main model.
    * $order = '{ModelClassName1}.column_name_1 ASC, {ModelClassName2}.column_name_2 DESC';
    * // Let the engine do all the ordering.
    * // Order fetched data by columns name listed as an array base First In First Out basis.
    * $order = array(
    *     '{ModelClassName1}.column_name_1',           // This column will be ordered first, then column_name_2 and so on.
    *     '{ModelClassName2}.column_name_2' => 'DESC', // Use array key as column name and array value as order mode.
    *     '{ModelClassName3}.column_name_3'            // Just define column name, and it will be ordered by ASC.
    * );
    *
    * // Limit fetched data with starting offset.
    * $limit = 25;
    * $offset = 0;
    *
    * // Define any custom sql to be included at the end of the auto generated sql process.
    * $custom = 'any custom sql to be included after auto-generated sql';
    *
    * // Define parameters' value with parameter name ":param_name" or with no name "?".
    * $param = array(
    *     'value_for_param_index_0',
    *     'param1' => 'value_for_param_name_param1',
    *     'value_for_param_index_1'
    * );
    *
    * @var string  $modelClassName  Model object or class name to be used as the main model.
    * @var array   $extension          Extending fetching to multiple models and levels.
    * @var array   $option          All possible parameters described above.
    * @return array
    */
   $modelMapper->loadModel('Post')


I need your input on this ASAP!
wendy
 
Posts: 43
Joined: Thu Jul 30, 2009 5:40 pm
Location: Indonesia

Re: Multiple Extension for Model

Postby leng » Mon Aug 10, 2009 5:05 pm

Whoa wendy, it seems like a good extension :D
How about writing a scenario on how this will be used so that more of us know more about this Model thing?
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm

Re: Multiple Extension for Model

Postby wendy » Tue Oct 13, 2009 9:24 am

leng wrote:Whoa wendy, it seems like a good extension :D
How about writing a scenario on how this will be used so that more of us know more about this Model thing?


Btw, Yii did a great job on the way how they implement their model with chained filter/validation and eager loading approach as well. I'm implementing quite a similar approach right now, though I'm still stuck on some recursive problem required in populating the data.
wendy
 
Posts: 43
Joined: Thu Jul 30, 2009 5:40 pm
Location: Indonesia

Previous

Return to Features Request

Who is online

Users browsing this forum: No registered users and 1 guest

cron