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

Use Doctrine metadata #247

Closed
mnapoli opened this issue Mar 4, 2014 · 12 comments
Closed

Use Doctrine metadata #247

mnapoli opened this issue Mar 4, 2014 · 12 comments

Comments

@mnapoli
Copy link

mnapoli commented Mar 4, 2014

Is there a way to avoid writing the same metadata twice: once for Doctrine, once for the Serializer?

I am thinking mainly about the property types. I am currently using YAML mapping for Doctrine, and YAML mapping to for the serializer.

@stof
Copy link
Contributor

stof commented Mar 4, 2014

The latest version is able to guess the metadata based on the Doctrine mapping if you register the driver: https://github.com/schmittjoh/serializer/blob/master/src/JMS/Serializer/Metadata/Driver/DoctrineTypeDriver.php

@mnapoli
Copy link
Author

mnapoli commented Mar 4, 2014

Great thanks! When you say latest version, you mean stable (0.15.0) or master?

And how do we use this driver? I can't find anything about it in the SerializerBuilder (at least in stable)?

@stof
Copy link
Contributor

stof commented Mar 4, 2014

it is available in 0.15.0.

Hmm, the SerializerBuilder indeed does not support registering the driver

@mnapoli
Copy link
Author

mnapoli commented Mar 4, 2014

If I create a PR adding SerializerBuilder::addDriver(Driver $driver) would that help? (it would create a DriverChain if 1 driver or more is manually registered, taking into account the current code)

@mnapoli
Copy link
Author

mnapoli commented Mar 4, 2014

or addMetadataDriver()? maybe that's better

@schmittjoh
Copy link
Owner

This is related to #232 - which is basically about that.

addDriver() will not work, we will need something like configureDriver($callback) as you need to wrap the current driver chain in the DoctrineTypeDriver.

@mnapoli
Copy link
Author

mnapoli commented Mar 4, 2014

OK should I close this one then?

@schmittjoh
Copy link
Owner

No, let's keep this open. It already has some more details.

@eddiejaoude
Copy link

Any ideas, when this functionality will be available? Thanks

@eddiejaoude
Copy link

👍

jaymecd pushed a commit to jaymecd/serializer that referenced this issue Mar 19, 2014
* commit 'c8a171357ca92b6706e395c757f334902d430ea9':
  fixes a regression
  moves driver creation to dedicated class (closes schmittjoh#247)
  Changed some constraint to allow latest versions
  Add support for XML namespace on the XML root element
  Added test for leading backslash in front of class name to TypeParserTest
  tests for accessor (public_method)
  Allow to fetch data from has*() with public_method
  Improve yaml documentacion Fix schmittjoh#100
@mnapoli
Copy link
Author

mnapoli commented Apr 7, 2014

FYI, here is how I managed to do it:

$builder = SerializerBuilder::create();
$builder->addMetadataDir(...)
    ->addMetadataDir(..., 'User\Domain');

$builder->setMetadataDriverFactory(new CallbackDriverFactory(function ($metadataDirs, Reader $reader) use ($container) {
    $driver = new DriverChain([
        new YamlDriver(new FileLocator($metadataDirs)),
        new AnnotationDriver($reader),
    ]);

    /** @var ManagerRegistry $em */
    $registry = $container->get(ManagerRegistry::class);
    return new DoctrineTypeDriver($driver, $registry);
}));

return $builder->build();

I think it's too bad it forces us to use the ManagerRegistry instead of the EntityManager directly because Doctrine doesn't ship with a default one. If you don't use Symfony you have write one, and that's seriously weird to implement (no documentation at all).

But anyway it seems to work, I have done only small tests for now. Thank you for implementing it @schmittjoh.

@schmittjoh
Copy link
Owner

I don't think we get around the registry requirement. A simple registry implementation is here:
doctrine/common#242

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

No branches or pull requests

4 participants