/** * This constructor initializes the class. * * @access public * @param DB_ORM_Model $model a reference to the implementing model * @param string $field the name of field in the database table * @throws Throwable_InvalidArgument_Exception indicates that an invalid field name * was specified */ public function __construct(DB_ORM_Model $model, $field) { if (!is_string($field) or $model->is_adaptor($field) or $model->is_alias($field) or !$model->is_field($field) or $model->is_relation($field)) { throw new Throwable_InvalidArgument_Exception('Message: Invalid field name defined. Reason: Field name either is not a field or is already defined.', array(':field' => $field)); } $this->model = $model; $this->metadata['field'] = $field; }
/** * This constructor initializes the class. * * @access public * @override * @param DB_ORM_Model $model a reference to the implementing model * @param array $metadata the relation's metadata */ public function __construct(DB_ORM_Model $model, array $metadata = array()) { parent::__construct($model, 'has_many'); // the parent model is the referenced table $parent_model = get_class($model); // Get parent model's name into variable, otherways a late static binding code throws a // syntax error when used like this: $this->metadata['parent_model']::primary_key() $this->metadata['parent_model'] = $parent_model; // the parent key (i.e. candidate key) is an ordered list of field names in the parent model $this->metadata['parent_key'] = isset($metadata['parent_key']) ? (array) $metadata['parent_key'] : $parent_model::primary_key(); // the through model is the pivot table if (isset($metadata['through_model'])) { $this->metadata['through_model'] = DB_ORM_Model::model_name($metadata['through_model']); } // the through keys is an array of two ordered lists of fields names: [0] matches with parent key and [1] matches with child key if (isset($metadata['through_keys'])) { $this->metadata['through_keys'] = (array) $metadata['through_keys']; } // the child model is the referencing table $this->metadata['child_model'] = DB_ORM_Model::model_name($metadata['child_model']); // the child key (i.e. foreign key) is an ordered list of field names in the child model $this->metadata['child_key'] = (array) $metadata['child_key']; // a set of options that will modify the query $this->metadata['options'] = isset($metadata['options']) ? (array) $metadata['options'] : array(); }
/** * This constructor instantiates this class. * * @access public */ public function __construct() { parent::__construct(); $this->fields = array('id' => new DB_ORM_Field_Integer($this, array('max_length' => 11, 'nullable' => FALSE, 'unsigned' => TRUE)), 'email' => new DB_ORM_Field_String($this, array('max_length' => 254, 'nullable' => FALSE)), 'username' => new DB_ORM_Field_String($this, array('default' => '', 'max_length' => 32, 'nullable' => FALSE)), 'password' => new DB_ORM_Field_String($this, array('max_length' => 64, 'nullable' => FALSE)), 'firstname' => new DB_ORM_Field_String($this, array('default' => NULL, 'max_length' => 35, 'nullable' => TRUE)), 'lastname' => new DB_ORM_Field_String($this, array('default' => NULL, 'max_length' => 50, 'nullable' => TRUE)), 'activated' => new DB_ORM_Field_Boolean($this, array('default' => TRUE, 'nullable' => FALSE)), 'banned' => new DB_ORM_Field_Boolean($this, array('default' => FALSE, 'nullable' => FALSE)), 'ban_reason' => new DB_ORM_Field_String($this, array('default' => NULL, 'max_length' => 255, 'nullable' => TRUE)), 'new_password_key' => new DB_ORM_Field_String($this, array('default' => NULL, 'max_length' => 64, 'nullable' => TRUE)), 'new_password_requested' => new DB_ORM_Field_Integer($this, array('default' => NULL, 'max_length' => 11, 'nullable' => TRUE)), 'new_email' => new DB_ORM_Field_String($this, array('default' => NULL, 'max_length' => 254, 'nullable' => TRUE)), 'new_email_key' => new DB_ORM_Field_String($this, array('default' => NULL, 'max_length' => 64, 'nullable' => TRUE)), 'logins' => new DB_ORM_Field_Integer($this, array('default' => 0, 'max_length' => 10, 'nullable' => FALSE, 'unsigned' => TRUE)), 'last_login' => new DB_ORM_Field_Integer($this, array('default' => NULL, 'max_length' => 10, 'nullable' => TRUE, 'unsigned' => TRUE)), 'last_ip' => new DB_ORM_Field_String($this, array('default' => NULL, 'max_length' => 39, 'nullable' => TRUE))); $this->adaptors = array('last_login_formatted' => new DB_ORM_Field_Adaptor_DateTime($this, array('field' => 'last_login')), 'new_password_requested_formatted' => new DB_ORM_Field_Adaptor_DateTime($this, array('field' => 'new_password_requested'))); $this->relations = array('user_roles' => new DB_ORM_Relation_HasMany($this, array('child_key' => array('user_id'), 'child_model' => 'User_Role', 'parent_key' => array('id'))), 'user_token' => new DB_ORM_Relation_HasMany($this, array('child_key' => array('user_id'), 'child_model' => 'User_Token', 'parent_key' => array('id')))); }
/** * This constructor initializes the class. * * @access public * @override * @param DB_ORM_Model $model a reference to the implementing model * @param array $metadata the relation's metadata */ public function __construct(DB_ORM_Model $model, array $metadata = array()) { parent::__construct($model, 'has_one'); // the parent model is the referenced table $parent_model = get_class($model); // Get parent model's name into variable, otherways a late static binding code throws a // syntax error when used like this: $this->metadata['parent_model']::primary_key() $this->metadata['parent_model'] = $parent_model; // the parent key (i.e. candidate key) is an ordered list of field names in the parent model $this->metadata['parent_key'] = isset($metadata['parent_key']) ? (array) $metadata['parent_key'] : $parent_model::primary_key(); // the child model is the referencing table $this->metadata['child_model'] = DB_ORM_Model::model_name($metadata['child_model']); // the child key (i.e. foreign key) is an ordered list of field names in the child model $this->metadata['child_key'] = (array) $metadata['child_key']; }
/** * This constructor instantiates this class using the specified model's name. * * @access public * @param string $model the model's name */ public function __construct($model) { $name = $model; $model = DB_ORM_Model::model_name($name); $this->data_source = DB_DataSource::instance($model::data_source(DB_DataSource::MASTER_INSTANCE)); $builder = 'DB_' . $this->data_source->dialect . '_Insert_Builder'; $this->builder = new $builder($this->data_source); $extension = DB_ORM_Model::builder_name($name); if (class_exists($extension)) { $this->extension = new $extension($this->builder); } $table = $model::table(); $this->builder->into($table); $this->model = $model; }
/** * This constructor instantiates this class. * * @access public */ public function __construct() { parent::__construct(); $this->fields = array('id' => new DB_ORM_Field_Integer($this, array('max_length' => 11, 'nullable' => FALSE, 'unsigned' => TRUE)), 'name' => new DB_ORM_Field_String($this, array('max_length' => 255, 'nullable' => FALSE)), 'description' => new DB_ORM_Field_String($this, array('max_length' => 255, 'nullable' => FALSE))); $this->relations = array('user_roles' => new DB_ORM_Relation_HasMany($this, array('child_key' => array('id'), 'child_model' => 'User_Role', 'parent_key' => array('id')))); }
/** * This constructor instantiates this class. * * @access public */ public function __construct() { parent::__construct(); $this->fields = array('id' => new DB_ORM_Field_String($this, array('max_length' => 24, 'nullable' => FALSE)), 'last_active' => new DB_ORM_Field_Integer($this, array('max_length' => 11, 'nullable' => FALSE)), 'contents' => new DB_ORM_Field_Text($this, array('nullable' => FALSE))); }
/** * This function returns the value associated with the specified property. * * @access public * @override * @param string $name the name of the property * @return mixed the value of the property * @throws Throwable_InvalidProperty_Exception indicates that the specified property is * either inaccessible or undefined */ public function __get($name) { switch ($name) { case 'ancestors': return $this->ancestors('ASC'); case 'children': return $this->children(); case 'descendants': return $this->descendants(); case 'first_child': return $this->children('ASC', 1); case 'last_child': return $this->children('DESC', 1); case 'leaves': return $this->leaves(); case 'level': return $this->level(); case 'parent': return $this->parent(); case 'path': return $this->path(); case 'siblings': return $this->siblings(); case 'root': return $this->root(); default: return parent::__get($name); } }
/** * This function returns an instance of the specified model. * * @access public * @static * @param string $model the model's name * @return mixed an instance of the specified model */ public static function factory($model) { $model = DB_ORM_Model::model_name($model); return new $model(); }
/** * This constructor instantiates this class. * * @access public */ public function __construct() { parent::__construct(); $this->fields = array('user_id' => new DB_ORM_Field_Integer($this, array('max_length' => 10, 'nullable' => FALSE, 'unsigned' => TRUE)), 'role_id' => new DB_ORM_Field_Integer($this, array('max_length' => 10, 'nullable' => FALSE, 'unsigned' => TRUE))); $this->relations = array('user' => new DB_ORM_Relation_BelongsTo($this, array('child_key' => array('user_id'), 'parent_key' => array('id'), 'parent_model' => 'User')), 'role' => new DB_ORM_Relation_BelongsTo($this, array('child_key' => array('role_id'), 'parent_key' => array('id'), 'parent_model' => 'Role'))); }
/** * This function saves the record matching using the primary key. * * @access public * @override * @param boolean $reload whether the model should be reloaded * after the save is done * @param boolean $mode TRUE=save, FALSE=update, NULL=automatic */ public function save($reload = FALSE, $mode = NULL) { $this->token = $this->create_token(); parent::save($reload, $mode); }
/** * This function returns an instance of the appropriate pre-compiler for the * specified model. * * @access public * @static * @param string $model the model's name * @return DB_SQL_Precompiler an instance of the pre-compiler */ public static function precompiler($model) { $model = DB_ORM_Model::model_name($model); $data_source = $model::data_source(DB_DataSource::MASTER_INSTANCE); $precompiler = 'DB_' . $data_source->dialect . '_Precompiler'; $object = new $precompiler($data_source); return $object; }
/** * This function logs the current user out. * * @access public * @param boolean $destroy whether the session is to be to completely * destroyed * @param boolean $logout_all whether all tokens for user are to be removed * @param boolean whether the logout was successful */ public function logout($destroy = FALSE, $logout_all = FALSE) { // Set by force_login() $this->_session->delete('auth_forced'); if ($token = Cookie::get('authautologin')) { // Delete the autologin cookie to prevent re-login Cookie::delete('authautologin'); // Clear the autologin token from the database $token = DB_ORM::select($this->models['token'])->where($this->columns['token'], DB_SQL_Operator::_EQUAL_TO_, $token)->limit(1)->query()->fetch(0); $token_model = DB_ORM_Model::model_name($this->models['token']); if ($logout_all) { DB_ORM::delete($this->models['token'])->where($this->columns['user_id'], DB_SQL_Operator::_EQUAL_TO_, $token->user)->execute(); } else { if ($token instanceof $token_model and $token->is_loaded()) { $token->delete(); } } } return parent::logout($destroy); }
/** * This constructor instantiates this class using the specified model's name. * * @access public * @param string $model the model's name * @param array $columns the columns to be selected */ public function __construct($model, array $columns = array()) { $name = $model; $model = DB_ORM_Model::model_name($name); $this->data_source = DB_DataSource::instance($model::data_source(DB_DataSource::SLAVE_INSTANCE)); $builder = 'DB_' . $this->data_source->dialect . '_Select_Builder'; $this->table = $model::table(); $this->builder = new $builder($this->data_source, $columns); if (empty($columns)) { $this->builder->all("{$this->table}.*"); } $this->builder->from($this->table); $extension = DB_ORM_Model::builder_name($name); if (class_exists($extension)) { $this->extension = new $extension($this->builder); } $this->model = $model; }