Sprig_Core::__construct PHP Метод

__construct() защищенный Метод

Initialize the fields and add validation rules based on field properties.
protected __construct ( ) : void
Результат void
    protected function __construct()
    {
        if ($this->_init) {
            if ($this->state() === 'loading') {
                // Object loading via mysql_fetch_object or similar has finished
                $this->state('loaded');
            }
            // Can only be called once
            return;
        }
        // Initialization has been started
        $this->_init = TRUE;
        // Set up the fields
        $this->_init();
        if (!$this->_model) {
            // Set the model name based on the class name
            $this->_model = strtolower(substr(get_class($this), 6));
        }
        if (!$this->_table) {
            // Set the table name to the plural model name
            $this->_table = inflector::plural($this->_model);
        }
        foreach ($this->_fields as $name => $field) {
            if ($field->primary === TRUE) {
                if (!$this->_primary_key) {
                    // This is the primary key
                    $this->_primary_key = $name;
                } else {
                    if (is_string($this->_primary_key)) {
                        // More than one primary key found, create a list of keys
                        $this->_primary_key = array($this->_primary_key);
                    }
                    // Add this key to the list
                    $this->_primary_key[] = $name;
                }
            }
        }
        foreach ($this->_fields as $name => $field) {
            if ($field instanceof Sprig_Field_ForeignKey and !$field->model) {
                if ($field instanceof Sprig_Field_HasMany) {
                    $field->model = Inflector::singular($name);
                } else {
                    $field->model = $name;
                }
            }
            if ($field instanceof Sprig_Field_ManyToMany) {
                if (!$field->through) {
                    // Get the model names for the relation pair
                    $pair = array(strtolower($this->_model), strtolower($field->model));
                    // Sort the model names alphabetically
                    sort($pair);
                    // Join the model names to get the relation name
                    $pair = implode('_', $pair);
                    if (!isset(Sprig::$_relations[$pair])) {
                        // Must set the pair key before loading the related model
                        // or we will fall into an infinite recursion loop
                        Sprig::$_relations[$pair] = TRUE;
                        $tables = array($this->table(), Sprig::factory($field->model)->table());
                        // Sort the table names alphabetically
                        sort($tables);
                        // Join the table names to get the table name
                        Sprig::$_relations[$pair] = implode('_', $tables);
                    }
                    // Assign by reference so that changes to the pivot table
                    // will carry over to all models
                    $field->through =& Sprig::$_relations[$pair];
                }
            }
            if (!$field->column) {
                // Create the key based on the field name
                if ($field instanceof Sprig_Field_BelongsTo) {
                    if (isset($field->foreign_key) and $field->foreign_key) {
                        $fk = $field->foreign_key;
                    } else {
                        $fk = Sprig::factory($field->model)->fk();
                    }
                    $field->column = $fk;
                } elseif ($field instanceof Sprig_Field_HasOne) {
                    $field->column = $this->fk();
                } elseif ($field instanceof Sprig_Field_ForeignKey) {
                    // This field is probably a Many and does not need a column
                } else {
                    $field->column = $name;
                }
            }
            if (!$field->label) {
                $field->label = Inflector::humanize($name);
            }
            if ($field->null) {
                // Fields that allow NULL values must accept empty values
                $field->empty = TRUE;
            }
            if ($field->editable) {
                if (!$field->empty and !isset($field->rules['not_empty'])) {
                    // This field must not be empty
                    $field->rules['not_empty'] = NULL;
                }
                if ($field->unique) {
                    // Field must be a unique value
                    $field->callbacks[] = array($this, '_unique_field');
                }
                if ($field->choices and !isset($field->rules['in_array'])) {
                    // Field must be one of the available choices
                    $field->rules['in_array'] = array(array_keys($field->choices));
                }
                if (!empty($field->min_length)) {
                    $field->rules['min_length'] = array($field->min_length);
                }
                if (!empty($field->max_length)) {
                    $field->rules['max_length'] = array($field->max_length);
                }
            }
            if ($field instanceof Sprig_Field_BelongsTo or !$field instanceof Sprig_Field_ForeignKey) {
                // Set the default value for any field that is stored in the database
                $this->_original[$name] = $field->value($field->default);
            }
        }
    }

Usage Example

Пример #1
0
 protected function __construct()
 {
     // Set the $_db to the static $_usedb
     // The static $_usedb thus determines which db future Sprig instances will have
     // Example: Sprig_Wi3::$_usedb = Database::instance("name", $config); $obj = Sprig_Wi3::factory(""); ... more Sprig_Wi3 models
     if (!empty(self::$_usedb)) {
         $this->_db = self::$_usedb;
     }
     // Now construct the Sprig
     return parent::__construct();
 }