Exemplo n.º 1
0
 /**
  * Converts the internal records into data used to generate a query.
  *
  * @return array
  */
 protected function _getRecords()
 {
     $fields = $values = $types = [];
     $columns = $this->_schema->columns();
     foreach ($this->records as $record) {
         $fields = array_merge($fields, array_intersect(array_keys($record), $columns));
     }
     $fields = array_values(array_unique($fields));
     foreach ($fields as $field) {
         $types[$field] = $this->_schema->column($field)['type'];
     }
     $default = array_fill_keys($fields, null);
     foreach ($this->records as $record) {
         $values[] = array_merge($default, $record);
     }
     return [$fields, $values, $types];
 }
 public function createTableSql(Table $table, $columns, $constraints, $indexes)
 {
     $content = array_merge($columns, $constraints);
     $content = implode(",\n", array_filter($content));
     $tableName = $table->name();
     if ($this->_driver->autoQuoting()) {
         $tableName = $this->_driver->quoteIdentifier($tableName);
     }
     $out = [sprintf("CREATE TABLE %s (\n%s\n)", $tableName, $content)];
     foreach ($indexes as $index) {
         $out[] = $index;
     }
     foreach ($table->columns() as $column) {
         $columnData = $table->column($column);
         if ($this->_driver->autoQuoting()) {
             $column = $this->_driver->quoteIdentifier($column);
         }
         if (isset($columnData['comment'])) {
             $out[] = sprintf('COMMENT ON COLUMN %s.%s IS %s', $tableName, $column, $this->_driver->schemaValue($columnData['comment']));
         }
     }
     return $out;
 }
Exemplo n.º 3
0
 /**
  * Generate String representation of Records
  *
  * @param \Cake\Database\Schema\Table $table Table schema array
  * @param int $recordCount The number of records to generate.
  * @return array Array of records to use in the fixture.
  */
 protected function _generateRecords(Table $table, $recordCount = 1)
 {
     $records = [];
     for ($i = 0; $i < $recordCount; $i++) {
         $record = [];
         foreach ($table->columns() as $field) {
             $fieldInfo = $table->column($field);
             $insert = '';
             switch ($fieldInfo['type']) {
                 case 'decimal':
                     $insert = $i + 1.5;
                     break;
                 case 'biginteger':
                 case 'integer':
                 case 'float':
                     $insert = $i + 1;
                     break;
                 case 'string':
                 case 'binary':
                     $isPrimary = in_array($field, $table->primaryKey());
                     if ($isPrimary) {
                         $insert = Text::uuid();
                     } else {
                         $insert = "Lorem ipsum dolor sit amet";
                         if (!empty($fieldInfo['length'])) {
                             $insert = substr($insert, 0, (int) $fieldInfo['length'] - 2);
                         }
                     }
                     break;
                 case 'timestamp':
                     $insert = time();
                     break;
                 case 'datetime':
                     $insert = date('Y-m-d H:i:s');
                     break;
                 case 'date':
                     $insert = date('Y-m-d');
                     break;
                 case 'time':
                     $insert = date('H:i:s');
                     break;
                 case 'boolean':
                     $insert = 1;
                     break;
                 case 'text':
                     $insert = "Lorem ipsum dolor sit amet, aliquet feugiat.";
                     $insert .= " Convallis morbi fringilla gravida,";
                     $insert .= " phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin";
                     $insert .= " venenatis cum nullam, vivamus ut a sed, mollitia lectus. Nulla";
                     $insert .= " vestibulum massa neque ut et, id hendrerit sit,";
                     $insert .= " feugiat in taciti enim proin nibh, tempor dignissim, rhoncus";
                     $insert .= " duis vestibulum nunc mattis convallis.";
                     break;
                 case 'uuid':
                     $insert = Text::uuid();
                     break;
             }
             $record[$field] = $insert;
         }
         $records[] = $record;
     }
     return $records;
 }
Exemplo n.º 4
0
 /**
  * {@inheritDoc}
  */
 public function createTableSql(Table $table, $columns, $constraints, $indexes)
 {
     $content = array_merge($columns, $constraints);
     $content = implode(",\n", array_filter($content));
     $tableName = $this->_driver->quoteIfAutoQuote($table->name());
     $temporary = $table->temporary() ? ' TEMPORARY ' : ' ';
     $out = [];
     $out[] = sprintf("CREATE%sTABLE %s (\n%s\n)", $temporary, $tableName, $content);
     foreach ($indexes as $index) {
         $out[] = $index;
     }
     foreach ($table->columns() as $column) {
         $columnData = $table->column($column);
         if (isset($columnData['comment'])) {
             $out[] = sprintf('COMMENT ON COLUMN %s.%s IS %s', $tableName, $this->_driver->quoteIfAutoQuote($column), $this->_driver->schemaValue($columnData['comment']));
         }
     }
     $pk = $this->_getPrimaryKey($table);
     if ($pk) {
         $fieldName = $pk['columns'][0];
         $out = Hash::merge($out, $this->getCreateAutoincrementSql($fieldName, $table->name()));
     }
     return $out;
 }
Exemplo n.º 5
0
 public function get_data()
 {
     $this->auth();
     $this->layout = false;
     $this->render(false);
     $model_name = Inflector::camelize($this->request->data['model']);
     $Model = TableRegistry::get($model_name);
     $table = new Table(Inflector::tableize($this->request->data['model']));
     $cols = $table->columns();
     //die(var_dump($Model));
     $order = [];
     if ($Model->hasField('sort')) {
         $order[$model_name . '.sort'] = 'ASC';
     }
     if ($Model->hasField('name')) {
         $order[$model_name . '.name'] = 'ASC';
     }
     if (isset($this->request->data['order_field']) && isset($this->request->data['order_dir'])) {
         $order = [$model_name . '.' . $this->request->data['order_field'] => $this->request->data['order_dir']];
     }
     if (isset($this->request->data['belongsTo'])) {
         $conditions = [];
         foreach ($this->request->data['belongsTo'] as $key => $val) {
             if ($val === 'null') {
                 $val = null;
             }
             $conditions[$key] = $val;
         }
         $result = $Model->find('all', ['conditions' => $conditions, 'order' => $order])->toArray();
     } else {
         $result = $Model->find('all', ['order' => $order])->toArray();
     }
     if ($result) {
         die(json_encode(["data" => $result, "status" => "success"]));
     }
     die(json_encode(["data" => [], "status" => "fail"]));
 }
Exemplo n.º 6
0
 /**
  * Test construction with columns
  *
  * @return void
  */
 public function testConstructWithColumns()
 {
     $columns = ['id' => ['type' => 'integer', 'length' => 11], 'title' => ['type' => 'string', 'length' => 255]];
     $table = new Table('articles', $columns);
     $this->assertEquals(['id', 'title'], $table->columns());
 }
 /**
  * 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)) {
         $content .= "            '_options' => [\n" . implode(', ', $options) . "\n            ],\n";
     }
     return "[\n{$content}        ]";
 }