/** * 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); }