-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add schema_tool service (replaces util/DoctrineHelper)
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters