Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a way to enable auto_mapping option using multipe entity managers #321

Merged
merged 1 commit into from
Oct 4, 2014
Merged

Added a way to enable auto_mapping option using multipe entity managers #321

merged 1 commit into from
Oct 4, 2014

Conversation

goetas
Copy link
Member

@goetas goetas commented Aug 11, 2014

Finally, I fond some time to create this PR. This should close #60

I've also added a small test. Feedbacks are welcome!

(if merged, this will require some lines on SF documentation...)

{
$definedBundles = array_keys($container->getParameter('kernel.bundles'));

foreach ($entityManagerConfigs as $name => &$entityManager) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd completely reverse the order of your foreach here. I'd loop through definedBundles and find an EM that is the candidate for mapping, not an O(n^3) solution. I assume that multi-auto_mapping should be verified somewhere else, not when deciding the bundles mapping.

It would turn your algorithm into something like this:

private function validateORMAutoMapping(array $entityManagerConfigs)
{
    $emAutoMapping = null;

    foreach ($entityManagerConfigs as $name => $entityManager) {
        if ( ! (isset($entityManager['auto_mapping']) && $entityManager['auto_mapping'])) {
            continue;
        }

        if ($emAutoMapping && $emAutoMapping !== $name) {
            throw new \LogicException(sprintf('You cannot enable "auto_mapping" on more than one entity manager at the same time (found in "%s" and %s").', $emAutoMapping, $name));
        }

        $emAutoMapping = $name;
    }
}

private function collectAutoMappings(array $entityManagerConfigs, array $bundles)
{
    foreach ($bundles as $bundle) {
        $emCandidate = null;

        foreach ($entityManagerConfigs as $name => &$entityManager) {
            // Specific bundle mapping
            if (isset($entityManager['mappings'][$bundle])) {
                $emCandidate = $entityManager;

                break;
            }

            // Check for auto-mapping
            if ( ! (isset($entityManager['auto_mapping']) && $entityManager['auto_mapping'])) {
                continue;
            }

            $emCandidate = $entityManager;
        }

        // If we could resolve at least one EM
        if ($emCandidate) {
            $emCandidate['mappings'][$bundle]['mapping'] = true;
        }
    }
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like a lot of complex logic, that imho should better be in the abstract base class, which is what I did. then again this solution is backwards compatible with earlier Symfony versions I guess.

@goetas
Copy link
Member Author

goetas commented Aug 11, 2014

I have committed the changes proposed by @guilhermeblanco

@@ -331,6 +331,10 @@ protected function ormLoad(array $config, ContainerBuilder $container)

$container->setAlias('doctrine.orm.entity_manager', sprintf('doctrine.orm.%s_entity_manager', $config['default_entity_manager']));


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra new line

@lsmith77
Copy link
Member

the question is if this code should not get pushed to the DoctrineBridge in Symfony, since the ODM bundles will need this code too.

@goetas
Copy link
Member Author

goetas commented Aug 15, 2014

Unfortunately I have zero experience with odm, but Monday I will try to take a look!

@goetas
Copy link
Member Author

goetas commented Aug 18, 2014

@lsmith77 I can't see a clean solution how to move this code to DoctrineBridge...

My first idea was to move validateORMAutoMapping and collectAutoMappings to DoctrineBridge, marking them as protected. But this solution introduces an extra dependency between DoctrineBridge and DoctrineBundle... and still need a change into DoctrineBundle.

First implementation:
DoctrineBundle: https://github.com/goetas/DoctrineBundle/compare/auto-mapping-alternative
DoctrineBridge: https://github.com/goetas/symfony/compare/auto-mapping
DoctrineMongoDBBundle: ...

Do you have some suggestions?

@stof
Copy link
Member

stof commented Aug 18, 2014

@goetas DoctrineBundle already depends on DoctrineBridge currently.

@goetas
Copy link
Member Author

goetas commented Aug 18, 2014

@stof
Is acceptable the way used in https://github.com/goetas/DoctrineBundle/compare/auto-mapping-and
https://github.com/goetas/symfony/compare/auto-mapping ?

Can it be merged (in the next future)?

@joschi127
Copy link

👍

$emAutoMapping = null;

foreach ($entityManagerConfigs as $name => $entityManager) {
if ( !isset($entityManager['auto_mapping']) || !$entityManager['auto_mapping']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the isset is useless. It will always be set thanks to the way the Configuration class normalizes the config.

@goetas
Copy link
Member Author

goetas commented Sep 1, 2014

Hi!
I have modified this PR, now it requires the merging of symfony/symfony#11815, but it adds the ability to add the same functionality to DoctrineMongoDBBundle (see doctrine/DoctrineMongoDBBundle#267).

@lsmith77
Copy link
Member

for BC you should do something like what I did in doctrine/DoctrinePHPCRBundle#167

@goetas
Copy link
Member Author

goetas commented Sep 10, 2014

why not add ~2.6 as min version in composer.json?

@stof
Copy link
Member

stof commented Sep 10, 2014

@goetas I don't want to need supporting 2 versions of the bundle. We need to keep supporting the 2.3 LTS until its EOL

@goetas
Copy link
Member Author

goetas commented Sep 10, 2014

Added the BC commit

fabpot added a commit to symfony/symfony that referenced this pull request Sep 23, 2014
…ping feature (goetas)

This PR was squashed before being merged into the 2.6-dev branch (closes #11815).

Discussion
----------

Added some methods to improve the handling of auto_mapping feature

| Q             | A
| ------------- | ---
| Bug fix?      |no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?  | yes
| Fixed tickets | doctrine/DoctrineBundle#60
| License       | MIT

This PR is a part that is required by:
* doctrine/DoctrineMongoDBBundle#267
* doctrine/DoctrineBundle#321

The proposed PRs are an alternative to #11650 and doctrine/DoctrineBundle#322

Commits
-------

2e30a43 Added some methods to improve the handling of auto_mapping feature
fabpot added a commit to symfony/doctrine-bridge that referenced this pull request Sep 23, 2014
…ping feature (goetas)

This PR was squashed before being merged into the 2.6-dev branch (closes #11815).

Discussion
----------

Added some methods to improve the handling of auto_mapping feature

| Q             | A
| ------------- | ---
| Bug fix?      |no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?  | yes
| Fixed tickets | doctrine/DoctrineBundle#60
| License       | MIT

This PR is a part that is required by:
* doctrine/DoctrineMongoDBBundle#267
* doctrine/DoctrineBundle#321

The proposed PRs are an alternative to symfony/symfony#11650 and doctrine/DoctrineBundle#322

Commits
-------

2e30a43 Added some methods to improve the handling of auto_mapping feature
@lsmith77
Copy link
Member

do your tests require 2.6?

https://travis-ci.org/doctrine/DoctrineBundle/jobs/34889877#L189

@goetas
Copy link
Member Author

goetas commented Sep 23, 2014

@lsmith77 I think that i should skip the test if DoctrineExtension does not have the fixManagersAutoMappings method. Might it work?

@lsmith77
Copy link
Member

yup

@goetas
Copy link
Member Author

goetas commented Sep 23, 2014

Squashed and tested against 2.6

@lsmith77
Copy link
Member

ready to merge then ..

@lsmith77
Copy link
Member

@stof seems to be like you are the chief merger on this Bundle .. I do have the shiny green button but do not want to get into the way of things.


if (!method_exists($loader, 'fixManagersAutoMappings')) {
$this->markTestSkipped('Auto mapping with multiple managers available with Symfony ~2.6');
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the return statement should be removed. It is dead code (markTestSkipped is throwing an exception to interrupt the test)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, actually, I will do it myself.

@stof
Copy link
Member

stof commented Oct 4, 2014

It took time, but here we go, this is in now. Thank you very much @goetas.

@stof stof merged commit 18fc82f into doctrine:master Oct 4, 2014
stof added a commit that referenced this pull request Oct 4, 2014
…entity managers (goetas)

This PR was merged into the 1.3.x-dev branch.

Discussion
----------

Added a way to enable auto_mapping option using multipe entity managers

Finally, I fond some time to create this PR. This should close #60

I've also added  a small test. Feedbacks are welcome!

(if merged, this will require some lines on SF documentation...)

Commits
-------

18fc82f Added a way to enable auto_mapping option using multipe entity managers
@goetas
Copy link
Member Author

goetas commented Oct 4, 2014

Thanks everybody!

@joschi127
Copy link

A huge thank you!

@joschi127
Copy link

@stof: do you know if this will make it into 1.3 and will there be a new 1.3 beta soon? Once again, thanks a lot!

@phackwer
Copy link

People, I can't find how to activate that for Symfony use. Any suggestions? Is there any new docs about how to do it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A way to enable auto_mapping with multipe Entity Manager
6 participants