/** * Generates a string representation of a schema. * * @param \Cake\Database\Schema\Table $table Table schema * @return string fields definitions */ protected function _generateSchema(Table $table) { $cols = $indexes = $constraints = []; foreach ($table->columns() as $field) { $fieldData = $table->column($field); $properties = implode(', ', $this->_values($fieldData)); $cols[] = " '{$field}' => [{$properties}],"; } foreach ($table->indexes() as $index) { $fieldData = $table->index($index); $properties = implode(', ', $this->_values($fieldData)); $indexes[] = " '{$index}' => [{$properties}],"; } foreach ($table->constraints() as $index) { $fieldData = $table->constraint($index); $properties = implode(', ', $this->_values($fieldData)); $constraints[] = " '{$index}' => [{$properties}],"; } $options = $this->_values($table->options()); $content = implode("\n", $cols) . "\n"; if (!empty($indexes)) { $content .= " '_indexes' => [\n" . implode("\n", $indexes) . "\n ],\n"; } if (!empty($constraints)) { $content .= " '_constraints' => [\n" . implode("\n", $constraints) . "\n ],\n"; } if (!empty($options)) { foreach ($options as &$option) { $option = ' ' . $option; } $content .= " '_options' => [\n" . implode(",\n", $options) . "\n ],\n"; } return "[\n{$content} ]"; }
/** * Test adding different kinds of indexes. * * @return void */ public function testAddIndexTypes() { $table = new Table('articles'); $table->addColumn('id', 'integer')->addColumn('title', 'string')->addColumn('author_id', 'integer'); $table->addIndex('author_idx', ['columns' => ['author_id'], 'type' => 'index'])->addIndex('texty', ['type' => 'fulltext', 'columns' => ['title']]); $this->assertEquals(['author_idx', 'texty'], $table->indexes()); }