public function save_table_structure(Database_Table $table) { $handle = fopen($this->get_name(), 'w'); /* * The fields. */ fwrite($handle, "# The fields\n\n"); $fields = $table->get_fields(); foreach ($fields as $field) { #print_r($field); fwrite($handle, $field->get_name() . '.type=' . $field->get_type() . "\n"); fwrite($handle, $field->get_name() . '.null=' . ($field->can_be_null() ? 'Yes' : 'No') . "\n"); if ($field->has_default()) { fwrite($handle, $field->get_name() . '.default=' . $field->get_default() . "\n"); } fwrite($handle, "\n"); } /* * The keys. */ $keys = $table->get_keys(); foreach ($keys as $key) { } fclose($handle); }
protected function _tables() { // Prepare an array to hold tables $tables = array(); // Create a new database table with name and database $table = new Database_Table($this->_model->table(), $this->_db); // Get the model's primary keys as an array $model_pks = is_array($this->_model->pk()) ? $this->_model->pk() : array($this->_model->pk()); // Loop through each field within the model foreach ($this->_model->fields() as $field) { // Check if the field implaments the migratable field interface if ($field instanceof Sprig_Field_Migratable) { // Loop through each column in the field foreach ($field->columns() as $column) { // Add the column to the table $table->add_column($column); } } elseif ($field->in_db) { // If the field is unique if ($field->unique) { // Add a unique constraint to the table $table->add_constraint(new Database_Constraint_Unique($field->column)); } // Loop through every column in the model foreach ($this->_columns($field, $table) as $column) { // Add the column to the table $table->add_column($column); } } elseif ($field instanceof Sprig_Field_ManyToMany) { // ManyToMany fields also contain a pivot table $pivot = new Database_Table($field->through, $this->_db); // Get the columns associated with the first half $columns = $this->_columns(new Sprig_Field_BelongsTo(array('model' => $field->model)), $pivot); // Foreach column in the first half foreach ($columns as $column) { // Add it to the pivot table $pivot->add_column($column); } // Get the columns associated with the second half $columns = $this->_columns(new Sprig_Field_BelongsTo(array('model' => inflector::singular($this->_model->table()))), $pivot); // Foreach column in the second half foreach ($columns as $column) { // Add it to the pivot table $pivot->add_column($column); } // Add a primary key constraint on all fields within the pivot table $pivot->add_constraint(new Database_Constraint_Primary(array_keys($pivot->columns()), $pivot->name)); // Add the pivot table to the list of tables $tables[] = $pivot; } } // Add the primary key constraints to the table $table->add_constraint(new Database_Constraint_Primary($model_pks, $table->name)); // Add the table to the list $tables[] = $table; // And return all tables. return $tables; }
public function delete_by_id($id) { parent::delete_by_id($id); $cache_files = glob($this->get_cache_dir_name() . '/' . $id . '.*'); //echo 'print_r($cache_files): ' . "\n"; //print_r($cache_files); foreach ($cache_files as $cache_file) { unlink($cache_file); } }
public function get_field($field_name) { $fields = $this->get_fields(); if (preg_match('/^(\\w+)(?:__|\\.)(\\w+)$/', $field_name, $matches)) { $table_name = $matches[1]; $just_field_name = $matches[2]; if ($table_name == $this->get_name()) { #$field = parent::get_field($just_field_name); #$field->set_name("$table_name.$just_field_name"); #return $field; #return parent::get_field($field_name); return parent::get_field("{$table_name}.{$just_field_name}"); } else { $foreign_key_table_names = $this->get_foreign_key_table_names(); $database = $this->get_database(); foreach ($foreign_key_table_names as $f_k_t_n) { if ($f_k_t_n == $table_name) { $table = $database->get_table($table_name); $field = $table->get_field($just_field_name); $field->set_name("{$table_name}.{$just_field_name}"); return $field; } } } } else { #echo "\$field_name: $field_name\n"; #echo "Should prepend table_name?\n"; return parent::get_field($field_name); } throw new Exception("No field called {$field_name} in " . $this->get_name()); }
private function get_filename_for_table(Database_Table $table) { return $this->get_name() . '/' . $table->get_name() . '.txt'; }
public function get_last_edited_message(Database_Table $table, $id) { return 'Updated row ' . $id . ' from the ' . $table->get_name() . ' table.'; }
protected function _tables() { // Prepare an array to hold tables $tables = array(); // Create a new database table with name and database $table = new Database_Table($this->_model->table(), $this->_db); // Get the model's primary keys as an array $model_pks = is_array($this->_model->primary_key()) ? $this->_model->primary_key() : array($this->_model->primary_key()); // Loop through each field within the model foreach ($this->_model->fields() as $field) { // Check if the field implaments the migratable field interface if ($field instanceof Jelly_Field_Migratable) { // Loop through each column in the field foreach ($field->columns() as $column) { // Add the column to the table $table->add_column($column); } } elseif ($field->in_db) { // If the field is unique if ($field->unique) { // Add a unique constraint to the table $table->add_constraint(new Database_Constraint_Unique($field->column)); } // Loop through every column in the model foreach ($this->_columns($field, $table) as $column) { // Add the column to the table $table->add_column($column); } } elseif ($field instanceof Jelly_Field_ManyToMany) { // ManyToMany fields also contain a pivot table $pivot = new Database_Table($field->through['model'], $this->_db); // Get fields $columns = $field->through['columns']; foreach ($columns as $field) { // Chekt if the field names are defaults if (strstr($field, ':')) { list($model, $field) = explode(':', $field); // Append the : back onto $field, it's key for recognizing the alias below $field = ':' . $field; // We should be able to find a valid meta object here if (FALSE == ($meta = Jelly::meta($model))) { throw new Kohana_Exception('Meta data for :model was not found while trying to resolve :field', array(':model' => $model, ':field' => $field)); } $field = $meta->foreign_key(); } $column = Database_Column::factory('int'); $column->auto_increment = FALSE; $column->name = $field; $cols[] = $column; } // Add to pivot foreach ($cols as $column) { // Add it to the pivot table $pivot->add_column($column); } // Add a primary key constraint on all fields within the pivot table $pivot->add_constraint(new Database_Constraint_Primary(array_keys($pivot->columns()), $pivot->name)); /** * @todo It would be more than appropriate to add a contstraint in a following * form into a database: ALTER TABLE `roles_users` ADD CONSTRAINT `roles_users_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE, * */ // Add the pivot table to the list of tables $tables[] = $pivot; } } // Add the primary key constraints to the table $table->add_constraint(new Database_Constraint_Primary($model_pks, $table->name)); // Add the table to the list $tables[] = $table; // And return all tables. return $tables; }
public function __construct(HTMLTags_URL $url, Database_Table $table, $order_by, $direction, $offset, $limit, $limit_str, $rows_html_table_caption, $lpnd_class, $data_table_class, $fields, $actions) { parent::__construct(); $table_renderer = $table->get_renderer(); /* * ---------------------------------------- * Check the inputs. * ---------------------------------------- */ /* * ---------------------------------------- * Create the content. * ---------------------------------------- */ $limit_previous_next_div = new HTMLTags_Div(); $limit_previous_next_div->set_attribute_str('class', $lpnd_class); /* * To allow the user to set the number of extras to show at a time. */ $limit_action = clone $url; $limit_action->delete_all_get_variables(); $limit_form = new Database_LimitForm($limit_action, $limit, $limit_str); $get_vars = $url->get_get_variables(); foreach (array_keys($get_vars) as $key) { $limit_form->add_hidden_input($key, $get_vars[$key]); } $limit_form->add_hidden_input('order_by', $order_by); $limit_form->add_hidden_input('direction', $direction); $limit_form->add_hidden_input('offset', $offset); $limit_previous_next_div->append_tag_to_content($limit_form); $previous_next_url = clone $url; $previous_next_url->set_get_variable('order_by', $order_by); $previous_next_url->set_get_variable('direction', $direction); $row_count = $table->count_all_rows(); $previous_next_ul = new Database_PreviousNextUL($previous_next_url, $offset, $limit, $row_count); $limit_previous_next_div->append_tag_to_content($previous_next_ul); $this->append_tag_to_content($limit_previous_next_div); # ------------------------------------------------------------------ /* * The table. */ $rows_html_table = new HTMLTags_Table(); $rows_html_table->set_attribute_str('class', $data_table_class); /* * The caption. */ $caption = new HTMLTags_Caption($rows_html_table_caption); $rows_html_table->append_tag_to_content($caption); /* * The Heading Row. */ $sort_href = clone $url; $sort_href->set_get_variable('limit', $limit); $heading_row = new Database_SortableHeadingTR($sort_href, $direction); //if (isset($fields_str)) { // $fields = array(); // // foreach (explode(' ', $fields_str) as $field_name) { // $fields[] = $table->get_field($field_name); // } //} else { // $fields = $table->get_fields(); //} foreach ($fields as $field) { if ($field['sortable']) { $heading_row->append_sortable_field_name($field['name']); } else { $heading_row->append_nonsortable_field_name($field['name']); } } foreach ($actions as $action) { $heading_row->append_tag_to_content($action['th']); } $rows_html_table->append_tag_to_content($heading_row); # ------------------------------------------------------------------ /* * Display the contents of the table. */ $rows = $table->get_all_rows($order_by, $direction, $offset, $limit); foreach ($rows as $row) { $row_renderer = $row->get_renderer(); $data_tr = new HTMLTags_TR(); foreach ($fields as $field) { $field_td = self::get_html_table_cell_for_field($field, $row_renderer); $data_tr->append_tag_to_content($field_td); } //foreach ( // $row_renderer->get_action_tds( // $actions_str, // $url // ) // as // $action_td //) { // $data_tr->append_tag_to_content($action_td); //} foreach ($actions as $action) { $action_td = self::get_html_table_cell_for_field($action, $row_renderer); $data_tr->append_tag_to_content($action_td); } $rows_html_table->append_tag_to_content($data_tr); } # ------------------------------------------------------------------ $this->append_tag_to_content($rows_html_table); $this->append_tag_to_content($limit_previous_next_div); }
/** * Remove all tables associated with the model if they already exist in the database. * * @return Migration */ public function remove() { // Loop through every table foreach ($this->_tables as $table) { // If the table exists if ($table = Database_Table::instance($table->name, $this->_db)) { // Then drop it DB::drop('table', $table->name)->execute($this->_db); } } // Return this for chaining return $this; }
public static function save_table_structure_in_directory(Database_Table $table, $table_specification_directory_name) { $debug = FALSE; #$debug = TRUE; $table_name = $table->get_name(); if ($debug) { echo '$table_name: ' . $table_name . PHP_EOL; } /* * Make sure that the dir exists. */ if (!is_dir($table_specification_directory_name)) { FileSystem_DirectoryHelper::mkdir_parents($table_specification_directory_name); } foreach ($db_table->get_fields() as $db_field) { $db_field_name = $db_field->get_name(); if ($debug) { echo '$db_field_name: ' . $db_field_name . PHP_EOL; } /* * Save the type. */ $db_field_type = $db_field->get_type(); if ($debug) { echo '$db_field_type: ' . $db_field_type . PHP_EOL; } $db_field_type_file_name = $table_specification_directory_name . DIRECTORY_SEPARATOR . $db_field_name . '.type'; if ($debug) { echo '$db_field_type_file_name: ' . $db_field_type_file_name . PHP_EOL; } if (!$debug) { if ($fh = fopen($db_field_type_file_name, 'w')) { fwrite($fh, $db_field_type . PHP_EOL); fclose($fh); } } /* * Save whether this field can be null or not */ $db_field_can_be_null = $db_field->can_be_null(); if ($debug) { echo '$db_field_can_be_null: ' . ($db_field_can_be_null ? 'YES' : 'NO') . PHP_EOL; } $db_field_can_be_null_file_name = $table_specification_directory_name . DIRECTORY_SEPARATOR . $db_field_name . '.can-be-null'; if ($debug) { echo '$db_field_can_be_null_file_name: ' . $db_field_can_be_null_file_name . PHP_EOL; } if (!$debug) { if ($fh = fopen($db_field_can_be_null_file_name, 'w')) { fwrite($fh, ($db_field_can_be_null ? 'YES' : 'NO') . PHP_EOL); fclose($fh); } } /* * Save the key */ $db_field_key = $db_field->get_key(); if (strlen($db_field_key) > 0) { if ($debug) { echo '$db_field_key: ' . $db_field_key . PHP_EOL; } $db_field_key_file_name = $table_specification_directory_name . DIRECTORY_SEPARATOR . $db_field_name . '.key'; if ($debug) { echo '$db_field_key_file_name: ' . $db_field_key_file_name . PHP_EOL; } if (!$debug) { if ($fh = fopen($db_field_key_file_name, 'w')) { fwrite($fh, $db_field_key . PHP_EOL); fclose($fh); } } } /* * Save the default */ $db_field_default = $db_field->get_default(); if (strlen($db_field_default) > 0) { if ($debug) { echo '$db_field_default: ' . $db_field_default . PHP_EOL; } $db_field_default_file_name = $table_specification_directory_name . DIRECTORY_SEPARATOR . $db_field_name . '.default'; if ($debug) { echo '$db_field_default_file_name: ' . $db_field_default_file_name . PHP_EOL; } if (!$debug) { if ($fh = fopen($db_field_default_file_name, 'w')) { fwrite($fh, $db_field_default . PHP_EOL); fclose($fh); } } } /* * Save any extra info. */ $db_field_extra = $db_field->get_extra(); if (strlen($db_field_extra) > 0) { if ($debug) { echo '$db_field_extra: ' . $db_field_extra . PHP_EOL; } $db_field_extra_file_name = $table_specification_directory_name . DIRECTORY_SEPARATOR . $db_field_name . '.extra'; if ($debug) { echo '$db_field_extra_file_name: ' . $db_field_extra_file_name . PHP_EOL; } if (!$debug) { if ($fh = fopen($db_field_extra_file_name, 'w')) { fwrite($fh, $db_field_extra . PHP_EOL); fclose($fh); } } } } #/* # * Save the indexes. # */ # #$dbh = DB::m(); # #$query = "SHOW INDEX FROM $table_name"; # #$result = mysql_query($query, $dbh); # #while ($row = mysql_fetch_assoc($result)) { # print_r($row); # # $index_file_name # = $table_specification_directory_name # . DIRECTORY_SEPARATOR . $row['Key_name'] . '.index'; # echo '$index_file_name: ' . $index_file_name . PHP_EOL; # # if ($fh = fopen($index_file_name, 'w')) { # foreach ( # explode(' ', 'Non_unique Column_name') # as # $key # ) { # fwrite( # $fh, # $key . ': ' . $row[$key] . PHP_EOL # ); # } # # fclose($fh); # } #} }