示例#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 the name of a possible behavior class
     $behavior_class = Jam::behavior_prefix() . Jam::capitalize_class_name($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 Jam_Behavior) {
             throw new Kohana_Exception('Behavior at index [ :key ] is not an instance of Jam_Behavior, :type found.', array(':type' => is_object($behavior) ? 'instance of ' . get_class($behavior) : gettype($behavior), ':key' => $name));
         }
         // Initialize behavior
         $behavior->initialize($this, $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->_defaults = array();
     if (!$this->_errors_filename) {
         // Default errors filename to the model's name
         $this->_errors_filename = 'validators/' . $this->_model;
     }
     // Table should be a sensible default
     if (empty($this->_table)) {
         $this->_table = Inflector::plural($model);
     }
     // Can we set a sensible foreign key?
     if (empty($this->_foreign_key)) {
         $this->_foreign_key = $model . '_id';
     }
     if (empty($this->_unique_key) and method_exists(Jam::class_name($this->_model), 'unique_key')) {
         $this->_unique_key = Jam::class_name($this->_model) . '::unique_key';
     }
     foreach ($this->_fields as $column => $field) {
         // Ensure a default primary key is set
         if ($field instanceof Jam_Field and $field->primary and empty($this->_primary_key)) {
             $this->_primary_key = $column;
         }
         // Ensure a default plymorphic key is set
         if ($field instanceof Jam_Field_Polymorphic and empty($this->_polymorphic_key)) {
             $this->_polymorphic_key = $column;
         }
     }
     foreach ($this->_associations as $column => &$association) {
         $association->initialize($this, $column);
     }
     foreach ($this->_fields as $column => &$field) {
         $field->initialize($this, $column);
         $this->_defaults[$column] = $field->default;
     }
     if (!$this->_collection and $class = Jam::collection_prefix() . Jam::capitalize_class_name($this->_model) and class_exists($class)) {
         $this->_collection = $class;
     }
     // Meta object is initialized and no longer writable
     $this->_initialized = TRUE;
     // Final meta callback
     $this->_events->trigger('meta.after_finalize', $this);
 }