Skip to content

Commit

Permalink
Add schema_tool service (replaces util/DoctrineHelper)
Browse files Browse the repository at this point in the history
  • Loading branch information
craigh committed Jun 18, 2015
1 parent 5c259ca commit a5afb74
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/lib/Zikula/Bundle/CoreBundle/Resources/config/core.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<parameter key="zikula.exception_listener.class">Zikula\Bundle\CoreBundle\EventListener\ExceptionListener</parameter>
<parameter key="zikula.output_compression_listener.class">Zikula\Bundle\CoreBundle\EventListener\OutputCompressionListener</parameter>
<parameter key="zikula.strip_front_controller_listener.class">Zikula\Bundle\CoreBundle\EventListener\StripFrontControllerListener</parameter>
<parameter key="zikula.doctrine.schema_tool.class">Zikula\Core\Doctrine\Helper\SchemaHelper</parameter>
</parameters>

<services>
Expand Down Expand Up @@ -155,5 +156,8 @@
<tag name="kernel.event_subscriber" />
</service>

<service id="zikula.doctrine.schema_tool" class="%zikula.doctrine.schema_tool.class%" lazy="true">
<argument type="service" id="doctrine.entitymanager" />
</service>
</services>
</container>
89 changes: 89 additions & 0 deletions src/lib/Zikula/Core/Doctrine/Helper/SchemaHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Copyright Zikula Foundation 2010 - Zikula Application Framework
*
* This work is contributed to the Zikula Foundation under one or more
* Contributor Agreements and licensed to You under the following license:
*
* @license MIT
*
* Please see the NOTICE file distributed with this source code for further
* information regarding copyright and licensing.
*/

namespace Zikula\Core\Doctrine\Helper;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;

/**
* Class SchemaHelper
* @package Zikula\Core\Doctrine\Helper
*/
class SchemaHelper
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
private $tool;

public function __construct(EntityManagerInterface $em)
{
$this->entityManager = $em;
$this->tool = new SchemaTool($em);
}

/**
* create tables from array of entity classes
* @param array $classes
* @throws \Doctrine\ORM\Tools\ToolsException
*/
public function create(array $classes)
{
$metaClasses = array();
foreach ($classes as $class) {
$metaClasses[] = $this->entityManager->getClassMetadata($class);
}
try {
$this->tool->createSchema($metaClasses);
} catch (\PDOException $e) {
throw $e;
}
}

/**
* drop tables from array of entity classes
* @param array $classes
*/
public function drop(array $classes)
{
$metaClasses = array();
foreach ($classes as $class) {
$metaClasses[] = $this->entityManager->getClassMetadata($class);
}
try {
$this->tool->dropSchema($metaClasses);
} catch (\PDOException $e) {
throw $e;
}
}

/**
* update tables from array of entity classes
* @param array $classes
* @param bool $saveMode
*/
public function update(array $classes, $saveMode=true)
{
$metaClasses = array();
foreach ($classes as $class) {
$metaClasses[] = $this->entityManager->getClassMetadata($class);
}
try {
$this->tool->updateSchema($metaClasses, $saveMode);
} catch (\PDOException $e) {
throw $e;
}
}
}
4 changes: 4 additions & 0 deletions src/lib/util/DoctrineHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
use Doctrine\ORM\EntityManager as EntityManager;
use Doctrine\ORM\Tools\SchemaTool as SchemaTool;

/**
* Class DoctrineHelper
* @deprecated to be removed at Core 2.0.0
*/
class DoctrineHelper
{
public static function createSchema(EntityManager $em, array $classes)
Expand Down

0 comments on commit a5afb74

Please sign in to comment.