protected function _compile_constraints() { // Let the parent do their stuff first $constraints = parent::_compile_constraints(); // Return the constraints return $constraints; }
protected function _compile_constraints() { // Let the parent process the constraints first. parent::_compile_constraints(); // Defaults given for datetimes if (isset($this->default)) { // If the default is a valid date format, then quote it, otherwise leave it as is $constraints['default'] = strtotime($this->default) ? $this->table->database->quote($this->default) : $this->default; } return $constraints; }
protected function _compile_constraints() { // Let the parent do their bit first $constraints = parent::_compile_constraints(); // If the field is set to auto_increment, then set it. if ($this->is_auto_increment) { $constraints[] = 'auto_increment'; } // Finally return the constraints return $constraints; }
/** * Gets the database columns associated with the field. * * @param Sprig_Field The sprig field. * @param Database_Table The parent database table. * @return array */ private function _columns(Sprig_Field $field, Database_Table $table) { // Foreign keys are represented as BelongTo relationships if ($field instanceof Sprig_Field_BelongsTo) { // Get the model the field references $references = $this->_model($field->model); // Get all the primary keys in the referenced model as an array $pks = is_array($references->pk()) ? $references->pk() : array($references->pk()); // Prepare a column array $columns = array(); // Loop through each primary key foreach ($pks as $pk) { // Get the foreign primary key field $foreign_field = $references->field($pk); // Generates a table column for that foreign field $column = current($this->_columns($foreign_field, $table)); // Renames the column to the standard foreign key format $column->name = Inflector::singular($references->table()) . '_' . $pk; // If the column is an integer if ($column instanceof Database_Column_Int) { // We must disable auto_increment $column->auto_increment = FALSE; } // Add the column to the column array $columns[] = $column; } // No further processing is needed, so return the columns. return $columns; } elseif ($field instanceof Sprig_Field_Char) { // If the character field is a text field if ($field instanceof Sprig_Field_Text) { // Set the column to have a text datatype $column = Database_Column::factory('text'); // TODO: Ugly hack, fix it. (Text datatypes dont take parameters). unset($column->max_length); } else { // Create a new database column $column = Database_Column::factory('varchar'); // Set the varchar's max length to a default of 45 $column->max_length = isset($field->max_length) ? $field->max_length : 64; } } elseif ($field instanceof Sprig_Field_Integer) { // Use the int datatype and create the column $column = Database_Column::factory('int'); // If the field is Sprig_Field_Auto then auto_increment is set to true. $column->auto_increment = $field instanceof Sprig_Field_Auto; } elseif ($field instanceof Sprig_Field_Boolean) { // Very simply, a tinyint is a 0 or a 1 $column = Database_Column::factory('tinyint'); } // Set the value of the column's name $column->name = $field->column; // Set the column's default value $column->default = $field->default; // The column is nullable if the field is not empty $column->nullable = !$field->empty; // Return the column as an array return array($column); }
/** * Gets the database columns associated with the field. * * @param Jelly_Field The Jelly field. * @param Database_Table The parent database table. * @return array */ private function _columns(Jelly_Field $field, Database_Table $table) { // print_r($field); // Break If the column is not present in database if (!$field->in_db) { return; } // switch (TRUE) { // Strings case $field instanceof Jelly_Field_String: case $field instanceof Jelly_Field_File: // Saves file upload path // Saves file upload path case $field instanceof Jelly_Field_Enum: // Handles lists. // all relationships below are extending the String field // // case ($field instanceof Jelly_Field_Slug): // case ($field instanceof Jelly_Field_Email): // case ($field instanceof Jelly_Field_Password): // // Password is 40 char long Sha-1 // Create a new database column $column = Database_Column::factory('varchar'); // Set the varchar's max length to a default of 64 // @todo More appropriate would be to check length before... if ($field instanceof Jelly_Field_Email or $field instanceof Jelly_Field_Slug) { $column->max_length = 255; } elseif ($field instanceof Jelly_Field_Password) { $column->max_length = 255; } elseif (empty($field->rules['max_length'][0])) { $column->max_length = 64; } else { $column->max_length = $field->rules['max_length'][0]; } break; // Integers // Integers case $field instanceof Jelly_Field_Integer: // Use the int datatype and create the column $column = Database_Column::factory('int'); // If the field is Jelly_Field_Auto then auto_increment is set to true. break; // Booleans // Booleans case $field instanceof Jelly_Field_Boolean: $column = Database_Column::factory('bool'); break; // Primary key -- can be an integer or string, but we presume its int // Primary key -- can be an integer or string, but we presume its int case $field instanceof Jelly_Field_Primary: $column = Database_Column::factory('int'); $column->auto_increment = TRUE; $column->unique = TRUE; // $column->primary = TRUE; break; // Texts // Texts case $field instanceof Jelly_Field_Text: case $field instanceof Jelly_Field_Serialized: // Handles serialized data if (isset($field->rules['max_length'][0])) { $lenght = $field->rules['max_length'][0]; switch (TRUE) { case $lenght <= 65535: $column = Database_Column::factory('text'); break; case $lenght <= 16777215: $column = Database_Column::factory('mediumtext'); break; case $lenght <= 4294967295: $column = Database_Column::factory('longtext'); break; } } else { $column = Database_Column::factory('text'); } // TODO: Ugly hack, fix it. (Text datatypes dont take parameters). unset($column->max_length); break; // Timestamps // Timestamps case $field instanceof Jelly_Field_Timestamp: $column = Database_Column::factory('timestamp'); break; // Floats // Floats case $field instanceof Jelly_Field_Float: $column = Database_Column::factory('float'); // Relationships // case ($field instanceof Jelly_Field_Relationship): // all relationships below are extending this field: // // case ($field instanceof Jelly_Field_ManyToMany): // case ($field instanceof Jelly_Field_HasMany): // case ($field instanceof Jelly_Field_HasOne): // Relationships // case ($field instanceof Jelly_Field_Relationship): // all relationships below are extending this field: // // case ($field instanceof Jelly_Field_ManyToMany): // case ($field instanceof Jelly_Field_HasMany): // case ($field instanceof Jelly_Field_HasOne): case $field instanceof Jelly_Field_BelongsTo: $column = Database_Column::factory('int'); $column->auto_increment = FALSE; $column->name = $field->column; if (!empty($field->default)) { $column->default = $field->default; } if (!empty($field->null)) { $column->nullable = (bool) $field->null; } /** * @todo It would be appropriate to add a contstraint : ALTER TABLE `roles_users` ADD CONSTRAINT `roles_users_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE, * */ return array($column); break; default: break; } // Set the remaining values of the column $column->name = $field->name; if (!empty($field->default)) { $column->default = $field->default; } if (!empty($field->null)) { $column->nullable = (bool) $field->null; } if (!empty($field->unique)) { $column->unique = $field->unique; } if (!empty($field->auto_increment)) { $column->auto_increment = $field->auto_increment; } // The column is nullable if the field is not empty // $column->nullable = $field->null;// ! $field->empty; // $column->unique = $field->unique; // Return the column as an array return array($column); }
public function reset() { // Get a list of columns from the database for this table $columns = $this->database->list_columns($this->name); // Reset the column array $this->_columns = array(); // Loop through each column, and add it to the column array foreach ($columns as $name => $column) { $this->_columns[$name] = Database_Column::instance($this, $name); } }