Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add Tableprefix feature #155

Closed
wants to merge 11 commits into from

2 participants

@exoon

If ['doctrine']['connection']['orm_default']['params']['tableprefix'] is set, it will used as tableprefix. Unfortunately I found no other way to get this feature. It would be create if the module can support it in the future.

@Ocramius
Owner

@exoon ok, the idea is neato, but there's some things to fix:

  1. I believe this does belong to the ORM Configuration settings (it's a metadata-specific problem, not connection related)
  2. move the listener to a new namespace DoctrineORMModule\Listener
  3. add tests that verify that the listener is attached to the event manager and that it has the correct prefix
  4. follow coding standard as of the other files in this namespace
  5. add relevant docs and eventually config keys (commented) as it currently happens in config/module.config.php

Nice idea so far =)

@exoon

Sorry for the many test commits. I really new with all the git stuff. Now I have my development machine synchronized with the fork and I am ready to start.

@Ocramius
Owner

@exoon no problem with the test commits. I can manually squash them later while merging

@exoon

I have done another commit.

to 1. : I don't understand
to 2. : listener is moved
to 3. : an empty test is added. I have not used TDD in the past but I am very interested in it. Do write the tests I need more time to learn.
to 4.: I hope I followed

I also added an option to change the naming strategy. The build in strategies are usable. See http://docs.doctrine-project.org/en/latest/reference/namingstrategy.html

As configurations strings I used: "underscore_case_lower" and "UNDERSCORE_CASE_UPPER". I don't know if it is according the standards.

src/DoctrineORMModule/Listener/TablePrefixListener.php
((6 lines not shown))
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*
+* This software consists of voluntary contributions made by many individuals
+* and is licensed under the MIT license. For more information, see
+* <http://www.doctrine-project.org>.
+*/
+
+// info: http://docs.doctrine-project.org/en/2.0.x/cookbook/sql-table-prefixes.html
@Ocramius Owner
Ocramius added a note

Remove this line. Eventually, reference @link http://docs.doctrine-project.org/en/latest/cookbook/sql-table-prefixes.html in the class docblock.

@exoon
exoon added a note

done with next commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/DoctrineORMModule/Listener/TablePrefixListener.php
((12 lines not shown))
+* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*
+* This software consists of voluntary contributions made by many individuals
+* and is licensed under the MIT license. For more information, see
+* <http://www.doctrine-project.org>.
+*/
+
+// info: http://docs.doctrine-project.org/en/2.0.x/cookbook/sql-table-prefixes.html
+
+namespace DoctrineORMModule\Listener;
+
+use \Doctrine\ORM\Event\LoadClassMetadataEventArgs;
+
+class TablePrefixListener
@Ocramius Owner
Ocramius added a note

Add class docblock

@exoon
exoon added a note

done with next commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/DoctrineORMModule/Listener/TablePrefixListener.php
((21 lines not shown))
+// info: http://docs.doctrine-project.org/en/2.0.x/cookbook/sql-table-prefixes.html
+
+namespace DoctrineORMModule\Listener;
+
+use \Doctrine\ORM\Event\LoadClassMetadataEventArgs;
+
+class TablePrefixListener
+{
+ protected $prefix = '';
+
+ public function __construct($prefix)
+ {
+ $this->prefix = (string) $prefix;
+ }
+
+ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
@Ocramius Owner
Ocramius added a note

Use PSR-2 standards. You can get it fixed automatically through php-cs-fixer

@exoon
exoon added a note

I don't see that php-cs-fixer changed something. Maybe everything was already correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
docs/configuration.md
@@ -93,6 +93,8 @@ return array(
'user' => 'root',
'password' => 'root',
'dbname' => 'crawler',
+ 'tableprefix' => 'prefix_',
@Ocramius Owner
Ocramius added a note

use 4 spaces instead of tabs

@exoon
exoon added a note

My editors setup is changed. Should work in the future.

@Ocramius Owner

Still needs fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
docs/configuration.md
@@ -93,6 +93,8 @@ return array(
'user' => 'root',
'password' => 'root',
'dbname' => 'crawler',
+ 'tableprefix' => 'prefix_',
+ 'nameing_strategy' => 'underscore_case_lower', // or 'UNDERSCORE_CASE_UPPER' everything else uses defaultNameingStrategy
@Ocramius Owner
Ocramius added a note

"nameing" -> "naming"

@exoon
exoon added a note

done with next commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
README.md
@@ -112,6 +112,8 @@ return array(
'user' => 'username',
'password' => 'password',
'dbname' => 'database',
+ 'tableprefix' => 'prefix_',
@Ocramius Owner
Ocramius added a note

Remove these from README.md. They're not needed in the homepage

@exoon
exoon added a note

done with next commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/DoctrineORMModule/Listener/TablePrefixListener.php
((26 lines not shown))
+
+class TablePrefixListener
+{
+ protected $prefix = '';
+
+ public function __construct($prefix)
+ {
+ $this->prefix = (string) $prefix;
+ }
+
+ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
+ {
+ $classMetadata = $eventArgs->getClassMetadata();
+ $classMetadata->setTableName($this->prefix . $classMetadata->getTableName());
+ foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
+ if ($mapping['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY) {
@Ocramius Owner
Ocramius added a note

Import ClassMetadataInfo

@exoon
exoon added a note

done with next commit

@Ocramius Owner

Missing docblock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/DoctrineORMModule/Service/EntityManagerFactory.php
((70 lines not shown))
- /**
- * {@inheritDoc}
- */
- public function getOptionsClass()
- {
- return 'DoctrineORMModule\Options\EntityManager';
- }
+ /**
+ * @todo make naming strategy interface useable
+ * http://docs.doctrine-project.org/en/latest/reference/namingstrategy.html
+ */
+ if (isset($cfg['nameing_strategy']))
+ {
+ switch ($cfg['nameing_strategy'])
+ {
+ case 'underscore_case_lower':
@Ocramius Owner
Ocramius added a note

Are these constants that can be found somewhere? (I'm seeing CASE_LOWER and CASE_UPPER some lines below)

@exoon
exoon added a note

UnderscoreNamingStrategy::CASE_LOWER ...
done with next commit

@Ocramius Owner
Ocramius added a note

@exoon no need to comment on all these... I'll see it directly if you push the commit ;)

@exoon
exoon added a note

makes sense :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/DoctrineORMModule/Service/EntityManagerFactory.php
((71 lines not shown))
- * {@inheritDoc}
- */
- public function getOptionsClass()
- {
- return 'DoctrineORMModule\Options\EntityManager';
- }
+ /**
+ * @todo make naming strategy interface useable
+ * http://docs.doctrine-project.org/en/latest/reference/namingstrategy.html
+ */
+ if (isset($cfg['nameing_strategy']))
+ {
+ switch ($cfg['nameing_strategy'])
+ {
+ case 'underscore_case_lower':
+ $namingStrategy = new \Doctrine\ORM\Mapping\UnderscoreNamingStrategy(CASE_LOWER);
@Ocramius Owner
Ocramius added a note

Import this class

@exoon
exoon added a note

done with next commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/DoctrineORMModule/Service/EntityManagerFactory.php
((81 lines not shown))
+ if (isset($cfg['nameing_strategy']))
+ {
+ switch ($cfg['nameing_strategy'])
+ {
+ case 'underscore_case_lower':
+ $namingStrategy = new \Doctrine\ORM\Mapping\UnderscoreNamingStrategy(CASE_LOWER);
+ $config->setNamingStrategy($namingStrategy);
+ break;
+ case 'UNDERSCORE_CASE_UPPER':
+ $namingStrategy = new \Doctrine\ORM\Mapping\UnderscoreNamingStrategy(CASE_UPPER);
+ $config->setNamingStrategy($namingStrategy);
+ break;
+ }
+ }
+
+ if (isset($cfg['tableprefix']) && $cfg['tableprefix'] != '')
@Ocramius Owner
Ocramius added a note

use is_string for the second check. An empty table prefix works too.

@exoon
exoon added a note

My plan was to avoid a listener when it is not needed (performance)

@Ocramius Owner
Ocramius added a note

If it's null, then it's already ok ;) If the user specifies an empty string, it's his fault. Don't overdo things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/DoctrineORMModule/Service/EntityManagerFactory.php
((85 lines not shown))
+ case 'underscore_case_lower':
+ $namingStrategy = new \Doctrine\ORM\Mapping\UnderscoreNamingStrategy(CASE_LOWER);
+ $config->setNamingStrategy($namingStrategy);
+ break;
+ case 'UNDERSCORE_CASE_UPPER':
+ $namingStrategy = new \Doctrine\ORM\Mapping\UnderscoreNamingStrategy(CASE_UPPER);
+ $config->setNamingStrategy($namingStrategy);
+ break;
+ }
+ }
+
+ if (isset($cfg['tableprefix']) && $cfg['tableprefix'] != '')
+ {
+ $evm = $connection->getEventManager();
+ $tablePrefixListener = new TablePrefixListener($cfg['tableprefix']);
+ $evm->addEventListener(\Doctrine\ORM\Events::loadClassMetadata, $tablePrefixListener);
@Ocramius Owner
Ocramius added a note

Import this class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
...ineORMModuleTest/Service/EntityManagerFactoryTest.php
((10 lines not shown))
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the MIT license. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace DoctrineORMModuleTest\Service;
+
+use PHPUnit_Framework_TestCase;
+
+class EntityManagerFactoryTest extends PHPUnit_Framework_TestCase
+{
@Ocramius Owner
Ocramius added a note

Tricky test. You will need to use a fake configuration and verify that the event manager has the correct attached subscriber.

@Ocramius Owner

Since your logic should probably be moved to the config factory, I think this test can go away

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...ineORMModuleTest/Service/EntityManagerFactoryTest.php
((11 lines not shown))
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the MIT license. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace DoctrineORMModuleTest\Service;
+
+use PHPUnit_Framework_TestCase;
+
+class EntityManagerFactoryTest extends PHPUnit_Framework_TestCase
+{
+}
@Ocramius Owner
Ocramius added a note

You will also need a new unit test that acts only on a ClassMetadataInfo mock and alters join table and table values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius
Owner
  1. It's ok as it is
  2. ok, now convert it to subscriber (see comments on code)
  3. You can look at the other tests or wait until someone adds them. The test suite will fail until the new code has coverage otherwise, which makes the PR not mergeable right now. Be patient if you can't write the tests yourself.
  4. Coding standard to follow is PSR-1. You should stop using tabs and replace those with 4 spaces instead. Otherwise, it's quite good already :)

About the naming strategy, please strip it from this PR and make a new one eventually. Don't mix things in the same PR.

@Ocramius
Owner

@exoon news on this one? :)

@exoon

Currently I stuck on writing the tests. I invested some time in phpunit the last days, but I am yet not ready to write them. I can try to clean my local commits and do a push later this day.

@Ocramius
Owner

@exoon consider re-doing the PR on a new branch eventually

@exoon

One test always fails. I hope rebasing my local commits worked so far (one Problem with a wrong commiter email was not solveable)

@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Listener/TablePrefixListener.php
@@ -0,0 +1,66 @@
+<?php
+
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
@Ocramius Owner

This header is messed up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Service/EntityManagerFactory.php
((4 lines not shown))
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * This software consists of voluntary contributions made by many individuals
- * and is licensed under the MIT license. For more information, see
- * <http://www.doctrine-project.org>.
- */
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
@Ocramius Owner

Header messed up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Listener/TablePrefixListener.php
((24 lines not shown))
+use \Doctrine\ORM\Event\LoadClassMetadataEventArgs;
+use \Doctrine\Common\EventSubscriber;
+use \Doctrine\ORM\Mapping\ClassMetadataInfo;
+
+/**
+ * Listener for used by \DoctrineORMModule\Service\ConfigurationFactory
+ * to enable tableprefixes
+ *
+ * @license MIT
+ * @link http://docs.doctrine-project.org/en/2.0.x/cookbook/sql-table-prefixes.html
+ * @author unknown - see @link
+ * @author Dominik Evers <exoon@online.de>
+ */
+class TablePrefixListener implements EventSubscriber
+{
+ protected $prefix = '';
@Ocramius Owner

Missing docblock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Listener/TablePrefixListener.php
((31 lines not shown))
+ *
+ * @license MIT
+ * @link http://docs.doctrine-project.org/en/2.0.x/cookbook/sql-table-prefixes.html
+ * @author unknown - see @link
+ * @author Dominik Evers <exoon@online.de>
+ */
+class TablePrefixListener implements EventSubscriber
+{
+ protected $prefix = '';
+
+ public function __construct($prefix)
+ {
+ $this->prefix = (string) $prefix;
+ }
+
+ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
@Ocramius Owner

Missing docblock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Listener/TablePrefixListener.php
((44 lines not shown))
+ }
+
+ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
+ {
+ $classMetadata = $eventArgs->getClassMetadata();
+ $classMetadata->setTableName($this->prefix . $classMetadata->getTableName());
+ foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
+ if ($mapping['type'] == ClassMetadataInfo::MANY_TO_MANY) {
+ $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
+ $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName;
+ }
+ }
+ }
+
+ /**
+ * (non-PHPdoc)
@Ocramius Owner

Simply use {@inheritDoc}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Listener/TablePrefixListener.php
((18 lines not shown))
+* <http://www.doctrine-project.org>.
+*
+*/
+
+namespace DoctrineORMModule\Listener;
+
+use \Doctrine\ORM\Event\LoadClassMetadataEventArgs;
+use \Doctrine\Common\EventSubscriber;
+use \Doctrine\ORM\Mapping\ClassMetadataInfo;
+
+/**
+ * Listener for used by \DoctrineORMModule\Service\ConfigurationFactory
+ * to enable tableprefixes
+ *
+ * @license MIT
+ * @link http://docs.doctrine-project.org/en/2.0.x/cookbook/sql-table-prefixes.html
@Ocramius Owner

Please use latest instead of 2.0.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Listener/TablePrefixListener.php
((14 lines not shown))
+* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*
+* This software consists of voluntary contributions made by many individuals
+* and is licensed under the MIT license. For more information, see
+* <http://www.doctrine-project.org>.
+*
+*/
+
+namespace DoctrineORMModule\Listener;
+
+use \Doctrine\ORM\Event\LoadClassMetadataEventArgs;
+use \Doctrine\Common\EventSubscriber;
+use \Doctrine\ORM\Mapping\ClassMetadataInfo;
+
+/**
+ * Listener for used by \DoctrineORMModule\Service\ConfigurationFactory
@Ocramius Owner

{@see \DoctrineORMModule\Service\ConfigurationFactory} instead of just the class name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Listener/TablePrefixListener.php
((34 lines not shown))
+ * @author unknown - see @link
+ * @author Dominik Evers <exoon@online.de>
+ */
+class TablePrefixListener implements EventSubscriber
+{
+ protected $prefix = '';
+
+ public function __construct($prefix)
+ {
+ $this->prefix = (string) $prefix;
+ }
+
+ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
+ {
+ $classMetadata = $eventArgs->getClassMetadata();
+ $classMetadata->setTableName($this->prefix . $classMetadata->getTableName());
@Ocramius Owner

Missing empty newline before this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Listener/TablePrefixListener.php
((35 lines not shown))
+ * @author Dominik Evers <exoon@online.de>
+ */
+class TablePrefixListener implements EventSubscriber
+{
+ protected $prefix = '';
+
+ public function __construct($prefix)
+ {
+ $this->prefix = (string) $prefix;
+ }
+
+ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
+ {
+ $classMetadata = $eventArgs->getClassMetadata();
+ $classMetadata->setTableName($this->prefix . $classMetadata->getTableName());
+ foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
@Ocramius Owner

Missing empty newline before this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Listener/TablePrefixListener.php
((36 lines not shown))
+ */
+class TablePrefixListener implements EventSubscriber
+{
+ protected $prefix = '';
+
+ public function __construct($prefix)
+ {
+ $this->prefix = (string) $prefix;
+ }
+
+ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
+ {
+ $classMetadata = $eventArgs->getClassMetadata();
+ $classMetadata->setTableName($this->prefix . $classMetadata->getTableName());
+ foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
+ if ($mapping['type'] == ClassMetadataInfo::MANY_TO_MANY) {
@Ocramius Owner

Please use Yoda conditions:

if (ClassMetadataInfo::MANY_TO_MANY === $mapping['type']) {

Also, avoid non-strict comparison (==) and always use === where possible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Listener/TablePrefixListener.php
((38 lines not shown))
+{
+ protected $prefix = '';
+
+ public function __construct($prefix)
+ {
+ $this->prefix = (string) $prefix;
+ }
+
+ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
+ {
+ $classMetadata = $eventArgs->getClassMetadata();
+ $classMetadata->setTableName($this->prefix . $classMetadata->getTableName());
+ foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
+ if ($mapping['type'] == ClassMetadataInfo::MANY_TO_MANY) {
+ $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
+ $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName;
@Ocramius Owner

Missing empty newline before this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Listener/TablePrefixListener.php
((37 lines not shown))
+class TablePrefixListener implements EventSubscriber
+{
+ protected $prefix = '';
+
+ public function __construct($prefix)
+ {
+ $this->prefix = (string) $prefix;
+ }
+
+ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
+ {
+ $classMetadata = $eventArgs->getClassMetadata();
+ $classMetadata->setTableName($this->prefix . $classMetadata->getTableName());
+ foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
+ if ($mapping['type'] == ClassMetadataInfo::MANY_TO_MANY) {
+ $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
@Ocramius Owner

Is the joinTable key defined on both sides?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Service/EntityManagerFactory.php
@@ -36,6 +38,33 @@ public function createService(ServiceLocatorInterface $sl)
$connection = $sl->get($options->getConnection());
$config = $sl->get($options->getConfiguration());
+ $cfg = $sl->get('Config');
@Ocramius Owner

Remove empty newline before this line, align = signs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Service/EntityManagerFactory.php
@@ -36,6 +38,33 @@ public function createService(ServiceLocatorInterface $sl)
$connection = $sl->get($options->getConnection());
$config = $sl->get($options->getConfiguration());
+ $cfg = $sl->get('Config');
+ $cfg = $cfg['doctrine']['connection']['orm_default']['params'];
@Ocramius Owner

orm_default is hardcoded here, should not be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Service/EntityManagerFactory.php
@@ -36,6 +38,33 @@ public function createService(ServiceLocatorInterface $sl)
$connection = $sl->get($options->getConnection());
$config = $sl->get($options->getConfiguration());
+ $cfg = $sl->get('Config');
+ $cfg = $cfg['doctrine']['connection']['orm_default']['params'];
+
+ /**
+ * @todo make naming strategy interface useable
+ * http://docs.doctrine-project.org/en/latest/reference/namingstrategy.html
+ */
+ if (isset($cfg['nameing_strategy'])) {
@Ocramius Owner

nameing => naming

@Ocramius Owner

Also, probably should be handled in the config factory, not in the entity manager factory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Service/EntityManagerFactory.php
@@ -36,6 +38,33 @@ public function createService(ServiceLocatorInterface $sl)
$connection = $sl->get($options->getConnection());
$config = $sl->get($options->getConfiguration());
+ $cfg = $sl->get('Config');
+ $cfg = $cfg['doctrine']['connection']['orm_default']['params'];
+
+ /**
+ * @todo make naming strategy interface useable
+ * http://docs.doctrine-project.org/en/latest/reference/namingstrategy.html
+ */
+ if (isset($cfg['nameing_strategy'])) {
+ switch ($cfg['nameing_strategy']) {
+ case 'underscore_case_lower':
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_LOWER);
+ $config->setNamingStrategy($namingStrategy);
@Ocramius Owner

Missing empty newline before this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Service/EntityManagerFactory.php
@@ -36,6 +38,33 @@ public function createService(ServiceLocatorInterface $sl)
$connection = $sl->get($options->getConnection());
$config = $sl->get($options->getConfiguration());
+ $cfg = $sl->get('Config');
+ $cfg = $cfg['doctrine']['connection']['orm_default']['params'];
+
+ /**
+ * @todo make naming strategy interface useable
+ * http://docs.doctrine-project.org/en/latest/reference/namingstrategy.html
+ */
+ if (isset($cfg['nameing_strategy'])) {
+ switch ($cfg['nameing_strategy']) {
+ case 'underscore_case_lower':
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_LOWER);
+ $config->setNamingStrategy($namingStrategy);
+ break;
@Ocramius Owner

Missing empty newline before this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Service/EntityManagerFactory.php
@@ -36,6 +38,33 @@ public function createService(ServiceLocatorInterface $sl)
$connection = $sl->get($options->getConnection());
$config = $sl->get($options->getConfiguration());
+ $cfg = $sl->get('Config');
+ $cfg = $cfg['doctrine']['connection']['orm_default']['params'];
+
+ /**
+ * @todo make naming strategy interface useable
+ * http://docs.doctrine-project.org/en/latest/reference/namingstrategy.html
+ */
+ if (isset($cfg['nameing_strategy'])) {
+ switch ($cfg['nameing_strategy']) {
+ case 'underscore_case_lower':
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_LOWER);
@Ocramius Owner

Use a class constant, not a global constant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Service/EntityManagerFactory.php
@@ -36,6 +38,33 @@ public function createService(ServiceLocatorInterface $sl)
$connection = $sl->get($options->getConnection());
$config = $sl->get($options->getConfiguration());
+ $cfg = $sl->get('Config');
+ $cfg = $cfg['doctrine']['connection']['orm_default']['params'];
+
+ /**
+ * @todo make naming strategy interface useable
+ * http://docs.doctrine-project.org/en/latest/reference/namingstrategy.html
+ */
+ if (isset($cfg['nameing_strategy'])) {
+ switch ($cfg['nameing_strategy']) {
+ case 'underscore_case_lower':
@Ocramius Owner

Magic constant, please use a class constant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Service/EntityManagerFactory.php
@@ -36,6 +38,33 @@ public function createService(ServiceLocatorInterface $sl)
$connection = $sl->get($options->getConnection());
$config = $sl->get($options->getConfiguration());
+ $cfg = $sl->get('Config');
+ $cfg = $cfg['doctrine']['connection']['orm_default']['params'];
+
+ /**
+ * @todo make naming strategy interface useable
+ * http://docs.doctrine-project.org/en/latest/reference/namingstrategy.html
+ */
+ if (isset($cfg['nameing_strategy'])) {
+ switch ($cfg['nameing_strategy']) {
+ case 'underscore_case_lower':
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_LOWER);
+ $config->setNamingStrategy($namingStrategy);
+ break;
+ case 'UNDERSCORE_CASE_UPPER':
@Ocramius Owner

Magic constant, please use a class constant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Service/EntityManagerFactory.php
@@ -36,6 +38,33 @@ public function createService(ServiceLocatorInterface $sl)
$connection = $sl->get($options->getConnection());
$config = $sl->get($options->getConfiguration());
+ $cfg = $sl->get('Config');
+ $cfg = $cfg['doctrine']['connection']['orm_default']['params'];
+
+ /**
+ * @todo make naming strategy interface useable
+ * http://docs.doctrine-project.org/en/latest/reference/namingstrategy.html
+ */
+ if (isset($cfg['nameing_strategy'])) {
+ switch ($cfg['nameing_strategy']) {
+ case 'underscore_case_lower':
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_LOWER);
+ $config->setNamingStrategy($namingStrategy);
+ break;
+ case 'UNDERSCORE_CASE_UPPER':
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_UPPER);
@Ocramius Owner

Use a class constant, not a global constant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Service/EntityManagerFactory.php
((4 lines not shown))
+ $cfg = $sl->get('Config');
+ $cfg = $cfg['doctrine']['connection']['orm_default']['params'];
+
+ /**
+ * @todo make naming strategy interface useable
+ * http://docs.doctrine-project.org/en/latest/reference/namingstrategy.html
+ */
+ if (isset($cfg['nameing_strategy'])) {
+ switch ($cfg['nameing_strategy']) {
+ case 'underscore_case_lower':
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_LOWER);
+ $config->setNamingStrategy($namingStrategy);
+ break;
+ case 'UNDERSCORE_CASE_UPPER':
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_UPPER);
+ $config->setNamingStrategy($namingStrategy);
@Ocramius Owner

Missing empty newline before this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Service/EntityManagerFactory.php
((5 lines not shown))
+ $cfg = $cfg['doctrine']['connection']['orm_default']['params'];
+
+ /**
+ * @todo make naming strategy interface useable
+ * http://docs.doctrine-project.org/en/latest/reference/namingstrategy.html
+ */
+ if (isset($cfg['nameing_strategy'])) {
+ switch ($cfg['nameing_strategy']) {
+ case 'underscore_case_lower':
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_LOWER);
+ $config->setNamingStrategy($namingStrategy);
+ break;
+ case 'UNDERSCORE_CASE_UPPER':
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_UPPER);
+ $config->setNamingStrategy($namingStrategy);
+ break;
@Ocramius Owner

Missing empty newline before this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Service/EntityManagerFactory.php
((12 lines not shown))
+ switch ($cfg['nameing_strategy']) {
+ case 'underscore_case_lower':
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_LOWER);
+ $config->setNamingStrategy($namingStrategy);
+ break;
+ case 'UNDERSCORE_CASE_UPPER':
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_UPPER);
+ $config->setNamingStrategy($namingStrategy);
+ break;
+ }
+ }
+
+ if (isset($cfg['tableprefix']) && is_string($cfg['tableprefix'])) {
+ $evm = $connection->getEventManager();
+ $tablePrefixListener = new TablePrefixListener($cfg['tableprefix']);
+ $evm->addEventSubscriber($tablePrefixListener);
@Ocramius Owner

Missing empty newline before this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Service/EntityManagerFactory.php
((13 lines not shown))
+ case 'underscore_case_lower':
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_LOWER);
+ $config->setNamingStrategy($namingStrategy);
+ break;
+ case 'UNDERSCORE_CASE_UPPER':
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_UPPER);
+ $config->setNamingStrategy($namingStrategy);
+ break;
+ }
+ }
+
+ if (isset($cfg['tableprefix']) && is_string($cfg['tableprefix'])) {
+ $evm = $connection->getEventManager();
+ $tablePrefixListener = new TablePrefixListener($cfg['tableprefix']);
+ $evm->addEventSubscriber($tablePrefixListener);
+ return EntityManager::create($connection, $config, $evm);
@Ocramius Owner

Missing empty newline before this line

@Ocramius Owner

Again, this should probably have been handled in the config factory, so that this conditional can be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Service/EntityManagerFactory.php
((10 lines not shown))
+ */
+ if (isset($cfg['nameing_strategy'])) {
+ switch ($cfg['nameing_strategy']) {
+ case 'underscore_case_lower':
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_LOWER);
+ $config->setNamingStrategy($namingStrategy);
+ break;
+ case 'UNDERSCORE_CASE_UPPER':
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_UPPER);
+ $config->setNamingStrategy($namingStrategy);
+ break;
+ }
+ }
+
+ if (isset($cfg['tableprefix']) && is_string($cfg['tableprefix'])) {
+ $evm = $connection->getEventManager();
@Ocramius Owner

Align = signs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
src/DoctrineORMModule/Listener/TablePrefixListener.php
@@ -0,0 +1,66 @@
+<?php
@Ocramius Owner

I'm wondering why we need a listener and this cannot be handled directly in the naming strategy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
...ineORMModuleTest/Listener/TablePrefixListenerTest.php
((12 lines not shown))
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the MIT license. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace DoctrineORMModuleTest\Listener;
+
+use DoctrineORMModule\Listener\TablePrefixListener;
+use PHPUnit_Framework_TestCase;
+
+class TablePrefixListenerTest extends PHPUnit_Framework_TestCase
+{
+
@Ocramius Owner

Remove this empty newline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
...ineORMModuleTest/Listener/TablePrefixListenerTest.php
((10 lines not shown))
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the MIT license. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace DoctrineORMModuleTest\Listener;
+
+use DoctrineORMModule\Listener\TablePrefixListener;
+use PHPUnit_Framework_TestCase;
+
+class TablePrefixListenerTest extends PHPUnit_Framework_TestCase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
...ineORMModuleTest/Listener/TablePrefixListenerTest.php
((33 lines not shown))
+ }
+
+ public function testIsEventSubscriper()
+ {
+ $this->assertInstanceOf('\Doctrine\Common\EventSubscriber', $this->tablePrefixListener);
+ }
+
+ public function testSubscribedEvents()
+ {
+ $subscribedEvents = $this->tablePrefixListener->getSubscribedEvents();
+ $count = count($subscribedEvents);
+
+ $this->assertEquals($count,1);
+ $this->assertEquals($subscribedEvents[0], 'loadClassMetadata');
+ }
+
@Ocramius Owner

Remove empty newline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
...ineORMModuleTest/Listener/TablePrefixListenerTest.php
((34 lines not shown))
+
+ public function testIsEventSubscriper()
+ {
+ $this->assertInstanceOf('\Doctrine\Common\EventSubscriber', $this->tablePrefixListener);
+ }
+
+ public function testSubscribedEvents()
+ {
+ $subscribedEvents = $this->tablePrefixListener->getSubscribedEvents();
+ $count = count($subscribedEvents);
+
+ $this->assertEquals($count,1);
+ $this->assertEquals($subscribedEvents[0], 'loadClassMetadata');
+ }
+
+}
@Ocramius Owner

Add empty newline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius
Owner

Closing for inactivity - also thinking the solved problem is an edge case that could fit a new module

@Ocramius Ocramius closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 31, 2012
  1. @exoon

    Create TablePrefix.php

    exoon authored
  2. @exoon
Commits on Jan 1, 2013
  1. @exoon
  2. @exoon
  3. @exoon

    Test: upload local changes

    exoon authored
  4. @exoon

    Upload test again

    exoon authored
  5. @exoon

    Another uplaod test

    exoon authored
  6. @exoon

    Test 6

    exoon authored
Commits on Jan 2, 2013
  1. @exoon
  2. @exoon

    missing files

    exoon authored
Commits on Jan 14, 2013
  1. @exoon

    Tests are missing

    usrr authored exoon committed
This page is out of date. Refresh to see the latest.
View
2  docs/configuration.md
@@ -93,6 +93,8 @@ return array(
'user' => 'root',
'password' => 'root',
'dbname' => 'crawler',
+ 'tableprefix' => 'prefix_',
@Ocramius Owner
Ocramius added a note

use 4 spaces instead of tabs

@exoon
exoon added a note

My editors setup is changed. Should work in the future.

@Ocramius Owner

Still needs fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 'naming_strategy' => 'underscore_case_lower', // or 'UNDERSCORE_CASE_UPPER' everything else uses defaultNameingStrategy
'driverOptions' => array(
1002 => 'SET NAMES utf8'
),
View
66 src/DoctrineORMModule/Listener/TablePrefixListener.php
@@ -0,0 +1,66 @@
+<?php
@Ocramius Owner

I'm wondering why we need a listener and this cannot be handled directly in the naming strategy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
@Ocramius Owner

This header is messed up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*
+* This software consists of voluntary contributions made by many individuals
+* and is licensed under the MIT license. For more information, see
+* <http://www.doctrine-project.org>.
+*
+*/
+
+namespace DoctrineORMModule\Listener;
+
+use \Doctrine\ORM\Event\LoadClassMetadataEventArgs;
+use \Doctrine\Common\EventSubscriber;
+use \Doctrine\ORM\Mapping\ClassMetadataInfo;
+
+/**
+ * Listener for used by \DoctrineORMModule\Service\ConfigurationFactory
@Ocramius Owner

{@see \DoctrineORMModule\Service\ConfigurationFactory} instead of just the class name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ * to enable tableprefixes
+ *
+ * @license MIT
+ * @link http://docs.doctrine-project.org/en/2.0.x/cookbook/sql-table-prefixes.html
@Ocramius Owner

Please use latest instead of 2.0.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ * @author unknown - see @link
+ * @author Dominik Evers <exoon@online.de>
+ */
+class TablePrefixListener implements EventSubscriber
+{
+ protected $prefix = '';
@Ocramius Owner

Missing docblock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ public function __construct($prefix)
@Ocramius Owner

Missing docblock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
+ $this->prefix = (string) $prefix;
+ }
+
+ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
@Ocramius Owner

Missing docblock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
+ $classMetadata = $eventArgs->getClassMetadata();
+ $classMetadata->setTableName($this->prefix . $classMetadata->getTableName());
@Ocramius Owner

Missing empty newline before this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
@Ocramius Owner

Missing empty newline before this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ if ($mapping['type'] == ClassMetadataInfo::MANY_TO_MANY) {
@Ocramius Owner

Please use Yoda conditions:

if (ClassMetadataInfo::MANY_TO_MANY === $mapping['type']) {

Also, avoid non-strict comparison (==) and always use === where possible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
@Ocramius Owner

Is the joinTable key defined on both sides?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName;
@Ocramius Owner

Missing empty newline before this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+ }
+ }
+
+ /**
+ * (non-PHPdoc)
@Ocramius Owner

Simply use {@inheritDoc}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ * @see \Doctrine\Common\EventSubscriber::getSubscribedEvents()
+ */
+ public function getSubscribedEvents()
+ {
+ return array('loadClassMetadata');
+ }
+}
View
59 src/DoctrineORMModule/Service/EntityManagerFactory.php
@@ -1,27 +1,29 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * This software consists of voluntary contributions made by many individuals
- * and is licensed under the MIT license. For more information, see
- * <http://www.doctrine-project.org>.
- */
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
@Ocramius Owner

Header messed up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*
+* This software consists of voluntary contributions made by many individuals
+* and is licensed under the MIT license. For more information, see
+* <http://www.doctrine-project.org>.
+*/
namespace DoctrineORMModule\Service;
use Doctrine\ORM\EntityManager;
use DoctrineModule\Service\AbstractFactory;
use Zend\ServiceManager\ServiceLocatorInterface;
+use DoctrineORMModule\Listener\TablePrefixListener;
+use \Doctrine\ORM\Mapping\UnderscoreNamingStrategy;
class EntityManagerFactory extends AbstractFactory
{
@@ -36,6 +38,33 @@ public function createService(ServiceLocatorInterface $sl)
$connection = $sl->get($options->getConnection());
$config = $sl->get($options->getConfiguration());
+ $cfg = $sl->get('Config');
@Ocramius Owner

Remove empty newline before this line, align = signs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $cfg = $cfg['doctrine']['connection']['orm_default']['params'];
@Ocramius Owner

orm_default is hardcoded here, should not be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ /**
+ * @todo make naming strategy interface useable
+ * http://docs.doctrine-project.org/en/latest/reference/namingstrategy.html
+ */
+ if (isset($cfg['nameing_strategy'])) {
@Ocramius Owner

nameing => naming

@Ocramius Owner

Also, probably should be handled in the config factory, not in the entity manager factory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ switch ($cfg['nameing_strategy']) {
+ case 'underscore_case_lower':
@Ocramius Owner

Magic constant, please use a class constant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_LOWER);
@Ocramius Owner

Use a class constant, not a global constant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $config->setNamingStrategy($namingStrategy);
@Ocramius Owner

Missing empty newline before this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ break;
@Ocramius Owner

Missing empty newline before this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ case 'UNDERSCORE_CASE_UPPER':
@Ocramius Owner

Magic constant, please use a class constant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $namingStrategy = new UnderscoreNamingStrategy(CASE_UPPER);
@Ocramius Owner

Use a class constant, not a global constant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $config->setNamingStrategy($namingStrategy);
@Ocramius Owner

Missing empty newline before this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ break;
@Ocramius Owner

Missing empty newline before this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+ }
+
+ if (isset($cfg['tableprefix']) && is_string($cfg['tableprefix'])) {
+ $evm = $connection->getEventManager();
@Ocramius Owner

Align = signs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $tablePrefixListener = new TablePrefixListener($cfg['tableprefix']);
+ $evm->addEventSubscriber($tablePrefixListener);
@Ocramius Owner

Missing empty newline before this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ return EntityManager::create($connection, $config, $evm);
@Ocramius Owner

Missing empty newline before this line

@Ocramius Owner

Again, this should probably have been handled in the config factory, so that this conditional can be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+
return EntityManager::create($connection, $config);
}
View
49 tests/DoctrineORMModuleTest/Listener/TablePrefixListenerTest.php
@@ -0,0 +1,49 @@
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the MIT license. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace DoctrineORMModuleTest\Listener;
+
+use DoctrineORMModule\Listener\TablePrefixListener;
+use PHPUnit_Framework_TestCase;
+
+class TablePrefixListenerTest extends PHPUnit_Framework_TestCase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+{
+
@Ocramius Owner

Remove this empty newline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ protected $tablePrefixListener;
+
+ public function setUp()
+ {
+ $this->tablePrefixListener = new TablePrefixListener('prefix');
+ }
+
+ public function testIsEventSubscriper()
+ {
+ $this->assertInstanceOf('\Doctrine\Common\EventSubscriber', $this->tablePrefixListener);
+ }
+
+ public function testSubscribedEvents()
+ {
+ $subscribedEvents = $this->tablePrefixListener->getSubscribedEvents();
+ $count = count($subscribedEvents);
+
+ $this->assertEquals($count,1);
+ $this->assertEquals($subscribedEvents[0], 'loadClassMetadata');
+ }
+
@Ocramius Owner

Remove empty newline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+}
View
30 tests/DoctrineORMModuleTest/Service/EntityManagerFactoryTest.php
@@ -0,0 +1,30 @@
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the MIT license. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace DoctrineORMModuleTest\Service;
+
+use PHPUnit_Framework_TestCase;
+
+class EntityManagerFactoryTest extends PHPUnit_Framework_TestCase
+{
@Ocramius Owner
Ocramius added a note

Tricky test. You will need to use a fake configuration and verify that the event manager has the correct attached subscriber.

@Ocramius Owner

Since your logic should probably be moved to the config factory, I think this test can go away

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ public function testAlwaysFail()
+ {
+ $this->assertNotNull(null);
+ }
+}
Something went wrong with that request. Please try again.