Skip to content

Instantly share code, notes, and snippets.

@dmvk
Created November 17, 2010 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmvk/703745 to your computer and use it in GitHub Desktop.
Save dmvk/703745 to your computer and use it in GitHub Desktop.
doctrine schema tool presenter for nette
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="description" content="Schema Tool">
<meta name="robots" content="{$robots}" n:ifset="$robots">
<title>Schema Tool</title>
<link rel="shortcut icon" href="{$basePath}/favicon.ico" type="image/x-icon">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="{$basePath}/js/netteAjax.js"></script>
</head>
<body>
<h1>Schema Tool</h1>
{snippet flash}
<div n:foreach="$flashes as $flash" class="flash {$flash->type}">{$flash->message}</div>
{/snippet}
<ul>
<li><a href="{plink getQuery!, create}" class="ajax">Create</a></li>
<li><a href="{plink getQuery!, update}" class="ajax">Update</a></li>
<li><a href="{plink getQuery!, drop}" class="ajax">Drop</a></li>
</ul>
{snippet dump}
{ifset $query}
<h2>{$statement} query dump, <a href="{plink executeQuery!, $statement}" class="ajax">execute</a></h2>
{? dump($query)}
{/ifset}
{/snippet}
<style type="text/css">
body {
font: 16px/1.5 "Trebuchet MS", "Geneva CE", lucida, sans-serif;
color: #333;
background-color: #fff;
}
h1 {
font-size: 150%;
color: #3484D2;
}
h2 {
font-size: 120%;
}
#ajax-spinner {
margin: 15px 0 0 15px;
padding: 13px;
background: white url('../images/spinner.gif') no-repeat 50% 50%;
font-size: 0;
z-index: 123456;
display: none;
}
div.flash {
color: black;
background: #FFF9D7;
border: 1px solid #E2C822;
padding: 1em;
margin: 1em 0;
}
div.error {
background: #FAD3C4;
border: 1px solid #D4C4C9;
}
a {
color: #3484D2;
}
a:hover {
background: #3484D2;
color: #fff;
}
</style>
<script type="text/javascript">
jQuery.nette.updateSnippet = function (id, html) {
$("#" + id).fadeTo("fast", 0.01, function () {
$(this).html(html).fadeTo("fast", 1);
});
};
$('a.ajax').live('click', function (event) {
event.preventDefault();
$.get(this.href);
});
</script>
</body>
</html>
<?php
/**
* Schema Tool presenter.
*
* @author David Morávek
*/
class SchemaPresenter extends \Nette\Application\Presenter
{
/**
* @var \Doctrine\ORM\EntityManager
*/
private $entityManager;
/**
* @var Doctrine\ORM\Tools\SchemaTool
*/
private $schemaTool;
/**
* @var array
*/
private $metadatas;
/**
* @return void
*/
protected function startup()
{
parent::startup();
$this->entityManager = Nette\Environment::getService('Doctrine\ORM\EntityManager');
$this->schemaTool = new Doctrine\ORM\Tools\SchemaTool($this->entityManager);
$this->metadatas = $this->entityManager->getMetadataFactory()->getAllMetadata();
}
/**
* Common render method.
* @return void
*/
protected function beforeRender()
{
parent::beforeRender();
$this->setLayout(FALSE);
}
/**
* Gets sql query
*
* @param string $schemaAction
*/
public function handleGetQuery($statement)
{
try {
switch ($statement) {
case 'create':
$this->template->query = $this->schemaTool->getCreateSchemaSql($this->metadatas);
break;
case 'update':
$this->template->query = $this->schemaTool->getUpdateSchemaSql($this->metadatas);
break;
case 'drop':
$this->template->query = $this->schemaTool->getDropSchemaSql($this->metadatas);
break;
default:
throw new InvalidArgumentException();
break;
}
$this->template->statement = $statement;
$this->invalidateControl();
} catch (Exception $e) {
$this->flashMessage($e->getMessage(), 'error');
$this->invalidateControl('flash');
}
if (!$this->isAjax())
$this->redirect('this');
}
/**
* Executes sql query
*
* @param string $schemaAction
*/
public function handleExecuteQuery($statement)
{
try {
switch ($statement) {
case 'create':
$this->schemaTool->createSchema($this->metadatas);
break;
case 'update':
$this->schemaTool->updateSchema($this->metadatas);
break;
case 'drop':
$this->schemaTool->dropSchema($this->metadatas);
break;
default:
throw new InvalidArgumentException();
break;
}
$this->flashMessage("$statement was successfully executed");
$this->invalidateControl();
} catch (Exception $e) {
$this->flashMessage($e->getMessage(), 'error');
$this->invalidateControl('flash');
}
if (!$this->isAjax())
$this->redirect('this');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment