Commit 7f628e36 authored by Avris's avatar Avris

v3.0

parent e635a96a
## Micrus Doctrine Bridge ##
This is a module for [Micrus framework](https://micrus.avris.it) that allows you to integrate it with [Doctrine ORM](http://www.doctrine-project.org/projects/orm.html).
This is a module for [Micrus framework](https://micrus.avris.it) that allows you
to integrate it with [Doctrine ORM](http://www.doctrine-project.org/projects/orm.html).
To install this module, open the file `app/Config/modules.yml` and add:
......@@ -16,7 +17,7 @@ Then run:
namespace App\Model;
use Avris\Micrus\Controller\Http\UploadedFile;
use Avris\Micrus\Model\Widget\ChoiceHelper;
use Avris\Micrus\Forms\Widget\ChoiceHelper;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
......@@ -241,7 +242,7 @@ Then run:
if an attribute `$fieldName` is not public, it will be accessed using `getFieldName` and `setFieldName`, or if it's an array:
`getFieldName`, `addFieldName`, `removeFieldName` and `hasFieldName`.
You can use Donctrine's EntityManager, retrieving it with `$this->getEm()` (in controllers) or `$container->get('orm')->getEntityManager()`.
You can use Doctrine's EntityManager, retrieving it with `$this->getEm()` (in controllers) or `$container->get('orm')->getEntityManager()`.
You can fetch repositories simply by the entity's name:
public function incrementStuffAction()
......@@ -269,3 +270,8 @@ Some Doctrine console commands are available in `bin/micrus` under `db` namespac
php bin/micrus db:query:dql "SELECT u FROM App\Model\User u WHERE u.role='ROLE_ADMIN'"
You can also use `php bin/mdoctrine` to access more advanced commands from Doctrine.
### Copyright ###
* **Author:** Andrzej Prusinowski [(Avris.it)](https://avris.it)
* **Licence:** [MIT](https://mit.avris.it)
......@@ -11,11 +11,11 @@
"homepage": "https://avris.it"
}],
"require": {
"avris/micrus": "^2.1",
"doctrine/orm": "^2.4"
"avris/micrus": "^3.0",
"doctrine/orm": "^2.5"
},
"autoload": {
"psr-4": { "Avris\\Micrus\\Doctrine\\": "src" }
},
"bin": ["mdoctrine"]
}
\ No newline at end of file
}
......@@ -2,11 +2,15 @@
namespace Avris\Micrus\Doctrine;
use Avris\Micrus\Console\ConsoleEvent;
use Avris\Micrus\Model\ModelDirectory;
use Avris\Micrus\Model\ORM;
use Avris\Bag\Bag;
use Avris\Micrus\Tool\Logger;
use Avris\Micrus\Tool\Config\ParametersProvider;
use Doctrine\Common\Cache\ClearableCache;
use Doctrine\Common\Proxy\Autoloader;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\ORM\Cache;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\ORM\Tools\Setup;
......@@ -19,7 +23,16 @@ class Doctrine implements ORM, ParametersProvider
/** @var string */
protected $cacheDir;
public function __construct(Logger $logger, $env, $rootDir, $db)
/**
* @param Logger $logger
* @param string $env
* @param string $rootDir
* @param array $db
* @param ModelDirectory[] $modelDirectories
* @param Cache $cache
* @throws \Doctrine\ORM\ORMException
*/
public function __construct(Logger $logger, $env, $rootDir, $db, array $modelDirectories, Cache $cache = null)
{
if (empty($db)) {
$logger->warning('No ORM configuration');
......@@ -30,11 +43,16 @@ class Doctrine implements ORM, ParametersProvider
$this->cacheDir = $rootDir . '/run/cache/' . $env . '/doctrine';
$dirs = [$rootDir . '/app/Model'];
foreach ($modelDirectories as $dir) {
$dirs[] = $dir->getDirectory();
}
$config = Setup::createAnnotationMetadataConfiguration(
[$rootDir . '/app/Model'],
$dirs,
$env == 'dev',
$this->cacheDir,
null,
$cache,
false
);
......@@ -114,6 +132,8 @@ class Doctrine implements ORM, ParametersProvider
public function onConsole(ConsoleEvent $event)
{
Autoloader::register($this->cacheDir, 'DoctrineProxies');
if (!$this->entityManager) {
return;
}
......
......@@ -8,13 +8,14 @@ abstract class DoctrineFixturesTask extends AbstractFixturesTask
protected function truncateDatabase()
{
$connection = $this->em->getConnection();
$schemaManager = $connection->getSchemaManager();
$platform = $connection->getDatabasePlatform();
$query = 'SET FOREIGN_KEY_CHECKS = 0;';
foreach ($schemaManager->listTables() as $table) {
$name = $table->getName();
$query .= 'TRUNCATE ' . $name . ';';
foreach ($connection->getSchemaManager()->listTables() as $table) {
$query .= $platform->getTruncateTableSQL($table->getName(), true) . ';';
}
$query .= 'SET FOREIGN_KEY_CHECKS = 1;';
$connection->executeQuery($query);
}
}
......@@ -16,7 +16,7 @@ class DoctrineLogger implements SQLLogger
public function startQuery($sql, array $params = null, array $types = null)
{
$this->logger->info($sql . ' | ' . @json_encode($params) . ' | ' . @json_encode($types));
$this->logger->notice('[SQL] ' . $sql . ' | ' . @json_encode($params) . ' | ' . @json_encode($types));
}
public function stopQuery()
......
......@@ -10,14 +10,21 @@ class DoctrineModule implements Module
return [
'services' => [
'orm' => [
'class' => __NAMESPACE__ . '\Doctrine',
'parameters' => ['@logger', '@env', '@rootDir', '@config.parameters.database'],
'class' => Doctrine::class,
'params' => [
'@logger',
'@env',
'@rootDir',
'@config.parameters.?database',
'#modelDirectory',
'@?doctrineCache',
],
'events' => ['cacheClear', 'cacheWarmup', 'console'],
'tags' => ['defaultParameters'],
],
'userProvider' => [
'class' => __NAMESPACE__ . '\DoctrineUserProvider',
'parameters' => ['@orm'],
'class' => DoctrineUserProvider::class,
'params' => ['@orm'],
],
],
];
......
......@@ -8,13 +8,17 @@ class DoctrineUserProvider implements UserProviderInterface
/** @var Doctrine */
protected $orm;
public function __construct(Doctrine $orm)
/** @var string */
protected $column;
public function __construct(Doctrine $orm, $column = 'email')
{
$this->orm = $orm;
$this->column = $column;
}
public function getUser($identifier)
{
return $this->orm->findOneBy('User', strpos($identifier, '@') === false ? 'username' : 'email', $identifier);
return $this->orm->findOneBy('User', $this->column, $identifier);
}
}
......@@ -2,44 +2,26 @@
namespace Avris\Micrus\Doctrine;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Repository\DefaultRepositoryFactory;
use Doctrine\ORM\Repository\RepositoryFactory;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ORM\Mapping\ClassMetadata;
class MicrusRepositoryFactory implements RepositoryFactory
{
/**
* @var ObjectRepository[]
*/
protected $repositoryList = [];
/** @var DefaultRepositoryFactory */
protected $defaultFactory;
public function getRepository(EntityManagerInterface $entityManager, $entityName)
public function __construct()
{
if (strpos($entityName, '\\') === false) {
$entityName = 'App\\Model\\' . ucfirst($entityName);
}
$repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_hash($entityManager);
if (isset($this->repositoryList[$repositoryHash])) {
return $this->repositoryList[$repositoryHash];
}
return $this->repositoryList[$repositoryHash] = $this->createRepository($entityManager, $entityName);
$this->defaultFactory = new DefaultRepositoryFactory();
}
/**
* @param EntityManagerInterface $entityManager
* @param string $entityName
* @return ObjectRepository
*/
protected function createRepository(EntityManagerInterface $entityManager, $entityName)
public function getRepository(EntityManagerInterface $entityManager, $entityName)
{
/* @var $metadata ClassMetadata */
$metadata = $entityManager->getClassMetadata($entityName);
$repositoryClassName = $metadata->customRepositoryClassName
?: $entityManager->getConfiguration()->getDefaultRepositoryClassName();
return new $repositoryClassName($entityManager, $metadata);
return $this->defaultFactory->getRepository(
$entityManager,
strpos($entityName, '\\') === false
? 'App\\Model\\' . ucfirst($entityName)
: $entityName
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment