Posts

Showing posts from January, 2014

CakePHP: Export / Download Database Code

/** * Dumps the MySQL database that this controller's model is attached to. * This action will serve the sql file as a download so that the user can save the backup to their local computer. * * @param string $tables Comma separated list of tables you want to download, or '*' if you want to download them all. */ public function db_mysql_dump($tables = '*') { $return = ''; $modelName = $this->modelClass; $dataSource = $this->{$modelName}->getDataSource(); $databaseName = $dataSource->getSchemaName(); // // Do a short header $return .= '-- Database: `' . $databaseName . '`' . "\n"; $return .= '-- Generation time: ' . date('D jS M Y H:i:s') . "\n\n\n"; if ($tables == '*') { $tables = array(); $result = $this->{$modelName}->query('SHOW TABLES'); foreach($result as $resultKey =&