- 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!
