write() публичный Метод

Writes schema file from object or options.
public write ( array | object $object, array $options = [] ) : mixed
$object array | object Schema object or options array.
$options array Schema object properties to override object.
Результат mixed False or string written to file.
 /**
  * Read database and Write schema object
  * accepts a connection as first arg or path to save as second arg
  *
  * @return void
  */
 public function generate()
 {
     $this->out(__d('cake_console', 'Generating Schema...'));
     $options = array();
     if ($this->params['force']) {
         $options['models'] = false;
     } elseif (!empty($this->params['models'])) {
         $options['models'] = String::tokenize($this->params['models']);
     }
     $snapshot = false;
     if (isset($this->args[0]) && $this->args[0] === 'snapshot') {
         $snapshot = true;
     }
     if (!$snapshot && file_exists($this->Schema->path . DS . $this->params['file'])) {
         $snapshot = true;
         $prompt = __d('cake_console', "Schema file exists.\n [O]verwrite\n [S]napshot\n [Q]uit\nWould you like to do?");
         $result = strtolower($this->in($prompt, array('o', 's', 'q'), 's'));
         if ($result === 'q') {
             return $this->_stop();
         }
         if ($result === 'o') {
             $snapshot = false;
         }
     }
     $cacheDisable = Configure::read('Cache.disable');
     Configure::write('Cache.disable', true);
     $content = $this->Schema->read($options);
     $content['file'] = $this->params['file'];
     Configure::write('Cache.disable', $cacheDisable);
     if ($snapshot === true) {
         $fileName = rtrim($this->params['file'], '.php');
         $Folder = new Folder($this->Schema->path);
         $result = $Folder->read();
         $numToUse = false;
         if (isset($this->params['snapshot'])) {
             $numToUse = $this->params['snapshot'];
         }
         $count = 0;
         if (!empty($result[1])) {
             foreach ($result[1] as $file) {
                 if (preg_match('/' . preg_quote($fileName) . '(?:[_\\d]*)?\\.php$/', $file)) {
                     $count++;
                 }
             }
         }
         if ($numToUse !== false) {
             if ($numToUse > $count) {
                 $count = $numToUse;
             }
         }
         $content['file'] = $fileName . '_' . $count . '.php';
     }
     if ($this->Schema->write($content)) {
         $this->out(__d('cake_console', 'Schema file: %s generated', $content['file']));
         $this->_stop();
     } else {
         $this->err(__d('cake_console', 'Schema file: %s generated'));
         $this->_stop();
     }
 }
Пример #2
0
 /**
  * Read database and Write schema object
  * accepts a connection as first arg or path to save as second arg
  *
  */
 public function generate()
 {
     $this->out(__('Generating Schema...'));
     $options = array();
     if (isset($this->params['force'])) {
         $options = array('models' => false);
     }
     $snapshot = false;
     if (isset($this->args[0]) && $this->args[0] === 'snapshot') {
         $snapshot = true;
     }
     if (!$snapshot && file_exists($this->Schema->path . DS . $this->params['file'])) {
         $snapshot = true;
         $result = strtolower($this->in("Schema file exists.\n [O]verwrite\n [S]napshot\n [Q]uit\nWould you like to do?", array('o', 's', 'q'), 's'));
         if ($result === 'q') {
             return $this->_stop();
         }
         if ($result === 'o') {
             $snapshot = false;
         }
     }
     $content = $this->Schema->read($options);
     $content['file'] = $this->params['file'];
     if ($snapshot === true) {
         $Folder = new Folder($this->Schema->path);
         $result = $Folder->read();
         $numToUse = false;
         if (isset($this->params['snapshot'])) {
             $numToUse = $this->params['snapshot'];
         }
         $count = 0;
         if (!empty($result[1])) {
             foreach ($result[1] as $file) {
                 if (preg_match('/schema(?:[_\\d]*)?\\.php$/', $file)) {
                     $count++;
                 }
             }
         }
         if ($numToUse !== false) {
             if ($numToUse > $count) {
                 $count = $numToUse;
             }
         }
         $fileName = rtrim($this->params['file'], '.php');
         $content['file'] = $fileName . '_' . $count . '.php';
     }
     if ($this->Schema->write($content)) {
         $this->out(__('Schema file: %s generated', $content['file']));
         $this->_stop();
     } else {
         $this->err(__('Schema file: %s generated'));
         $this->_stop();
     }
 }
 function admin_export($id = null)
 {
     $this->layout = null;
     $this->autoRender = false;
     if (!$id) {
         $this->Session->setFlash(__('Invalid Node Schema', true));
         $this->redirect(array('action' => 'index'));
     }
     $this->NodeSchema->recursive = -1;
     $this->set('node_schemas', $node_schemas = $this->NodeSchema->read(null, $id));
     App::Import('CakeSchema');
     $CakeSchema = new CakeSchema();
     //debug($CakeSchema->tables);
     //debug($CakeSchema->read(array('default', 'test')));
     // The above only works for tables that have models, our models are only instantiated when needed in memory
     $db =& ConnectionManager::getDataSource($node_schemas['NodeSchema']['datasource']);
     //$tables = array();
     $Model = new Model(null, $node_schemas['NodeSchema']['table_name'], $node_schemas['NodeSchema']['datasource']);
     $Model->name = $Model->alias = Inflector::classify($node_schemas['NodeSchema']['table_name']);
     $Object = ClassRegistry::init(array('class' => Inflector::pluralize($Model->name), 'ds' => $node_schemas['NodeSchema']['datasource']));
     // These cause issues for one reason or another
     unset($Object->tableToModel);
     unset($Object->__associationKeys);
     unset($Object->__associations);
     unset($Object->_findMethods);
     // The rest here just aren't needed, but don't cause any issues (not sure if it makes the export any faster, but makes the import php file smaller)
     unset($Object->Behaviors);
     unset($Object->useCache);
     unset($Object->cacheSources);
     unset($Object->alias);
     unset($Object->recursive);
     unset($Object->primaryKey);
     unset($Object->table);
     unset($Object->useTable);
     unset($Object->displayField);
     unset($Object->useDbConfig);
     // This may eventually get used, if I can figure how to set it so it writes to the file
     // This is weird and doesn't even seem right, but it renames the property and works
     $Object->{$node_schemas}['NodeSchema']['table_name'] = $Object->_schema;
     unset($Object->_schema);
     $CakeSchema->path = $this->nodeSchemaPath;
     $CakeSchema->file = $node_schemas['NodeSchema']['table_name'] . '.php';
     $CakeSchema->write($Object);
     if (file_exists($this->nodeSchemaPath . DS . $CakeSchema->file)) {
         $file = $this->nodeSchemaPath . DS . $CakeSchema->file;
         header("Content-Type: application/octet-stream");
         header("Accept-Ranges: bytes");
         header("Content-Length: " . filesize($file));
         header("Content-Disposition: attachment; filename=" . $CakeSchema->file);
         readfile($file);
     } else {
         $this->Session->setFlash(__('Could not export node schema, ensure the path: ' . $this->nodeSchemaPath . ' is writeable by the server.', true));
         $this->redirect(array('action' => 'index'));
     }
 }