Esempio n. 1
0
 /**
  * This is called after initialization to
  * finalize any changes to the meta object.
  *
  * @param  string  $model
  * @return
  */
 public function finalize($model)
 {
     if ($this->_initialized) {
         return;
     }
     // Set up event system
     $this->_events = new Jelly_Event($model);
     // Set the name of a possible behavior class
     $behavior_class = Jelly::behavior_prefix() . $model;
     // See if we have a special behavior class to use
     if (class_exists($behavior_class)) {
         // Load behavior
         $behavior = new $behavior_class();
         if (!in_array($behavior, $this->_behaviors)) {
             // Add to behaviors
             $this->_behaviors[] = $behavior;
         }
     }
     foreach ($this->_behaviors as $name => $behavior) {
         if (!$behavior instanceof Jelly_Behavior) {
             throw new Kohana_Exception('Behavior at index [ :key ] is not an instance of Jelly_Behavior, :type found.', array(':type' => is_object($behavior) ? 'instance of ' . get_class($behavior) : gettype($behavior), ':key' => $name));
         }
         // Initialize behavior
         $behavior->initialize($this->_events, $model, $name);
     }
     // Allow modification of this meta object by the behaviors
     $this->_events->trigger('meta.before_finalize', $this);
     // Ensure certain fields are not overridden
     $this->_model = $model;
     $this->_columns = $this->_defaults = $this->_field_cache = $this->_aliases = array();
     if (!$this->_errors_filename) {
         // Default errors filename to the model's name
         $this->_errors_filename = $this->_model;
     }
     // Table should be a sensible default
     if (empty($this->_table)) {
         $this->_table = Inflector::plural($model);
     }
     // See if we have a special builder class to use
     if (empty($this->_builder)) {
         $builder = Jelly::model_prefix() . 'builder_' . $model;
         if (class_exists($builder)) {
             $this->_builder = $builder;
         } else {
             $this->_builder = 'Jelly_Builder';
         }
     }
     // Can we set a sensible foreign key?
     if (empty($this->_foreign_key)) {
         $this->_foreign_key = $model . '_id';
     }
     // Initialize all of the fields with their column and the model name
     foreach ($this->_fields as $column => $field) {
         // Allow aliasing fields
         if (is_string($field)) {
             if (isset($this->_fields[$field])) {
                 $this->_aliases[$column] = $field;
             }
             // Aliases shouldn't pollute fields
             unset($this->_fields[$column]);
             continue;
         }
         $field->initialize($model, $column);
         // Ensure a default primary key is set
         if ($field->primary and empty($this->_primary_key)) {
             $this->_primary_key = $column;
         }
         // Search for a polymorphic key
         if (!empty($field->polymorphic)) {
             $this->_polymorphic_key = $field->name;
             // Add this class as a child if it hasn't been added yet
             if (!in_array($this->_model, $this->_children)) {
                 $this->_children[] = $this->_model;
             }
         }
         // Set the defaults so they're actually persistent
         $this->_defaults[$column] = $field->default;
     }
     // Meta object is initialized and no longer writable
     $this->_initialized = TRUE;
     // Final meta callback
     $this->_events->trigger('meta.after_finalize', $this);
 }
Esempio n. 2
0
 /**
  * Returns the model name of a class
  *
  * @param   string|Jelly_Model  $model
  * @return  string
  */
 public static function model_name($model)
 {
     if ($model instanceof Jelly_Model) {
         $model = get_class($model);
     }
     $prefix_length = strlen(Jelly::model_prefix());
     // Compare the first parts of the names and chomp if they're the same
     if (strtolower(substr($model, 0, $prefix_length)) === strtolower(Jelly::model_prefix())) {
         $model = substr($model, $prefix_length);
     }
     return strtolower($model);
 }
Esempio n. 3
0
 /**
  * This is called after initialization to
  * finalize any changes to the meta object.
  *
  * @return  void
  */
 public function finalize($model)
 {
     if ($this->initialized) {
         return;
     }
     // Ensure certain fields are not overridden
     $this->model = $model;
     $this->columns = $this->defaults = $this->field_cache = $this->aliases = array();
     // Table should be a sensible default
     if (empty($this->table)) {
         $this->table = inflector::plural($model);
     }
     // See if we have a special builder class to use
     if (empty($this->builder)) {
         $builder = Jelly::model_prefix() . 'builder_' . $model;
         if (class_exists($builder)) {
             $this->builder = $builder;
         } else {
             $this->builder = 'Jelly_Builder';
         }
     }
     // Can we set a sensible foreign key?
     if (empty($this->foreign_key)) {
         $this->foreign_key = $model . '_id';
     }
     // Initialize all of the fields with their column and the model name
     foreach ($this->fields as $column => $field) {
         // Allow aliasing fields
         if (is_string($field)) {
             if (isset($this->fields[$field])) {
                 $this->aliases[$column] = $field;
             }
             // Aliases shouldn't pollute fields
             unset($this->fields[$column]);
             continue;
         }
         $field->initialize($model, $column);
         // Ensure a default primary key is set
         if ($field->primary and empty($this->primary_key)) {
             $this->primary_key = $column;
         }
         // Set the defaults so they're actually persistent
         $this->defaults[$column] = $field->default;
         // Set the columns, so that we can access reverse database results properly
         if (!array_key_exists($field->column, $this->columns)) {
             $this->columns[$field->column] = array();
         }
         $this->columns[$field->column][] = $column;
     }
     // Meta object is initialized and no longer writable
     $this->initialized = TRUE;
 }