Skip to content

Instantly share code, notes, and snippets.

@fzaninotto
Created March 9, 2011 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fzaninotto/862369 to your computer and use it in GitHub Desktop.
Save fzaninotto/862369 to your computer and use it in GitHub Desktop.
<?php
// Propel way
$books = BookQuery::create()->find();
$xmlBooks = $books->toXml();
// Doctrine way
// $this is a symfony controller having access to the DIC
$em = $this->get('doctrine.orm.entity_manager');
$books = $em->createQuery('select b from \Acme\DemoBundle\Entity\Book b')->getResult();
$serializer = new \Symfony\Component\Serializer\Serializer();
$serializer->addNormalizer(new \Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer());
$serializer->setEncoder('xml', new \Symfony\Component\Serializer\Encoder\XmlEncoder());
$xmlBooks = $serializer->serialize($books, 'xml');
@beberlei
Copy link

beberlei commented Mar 9, 2011

This is only look "so big" if you don't abstract it. Doctrine doesn't ship with serialize by default, but you could easily write a base or helper class that handles this in a way to make it look like:

$books = $em->createQuery('..');
$xmlBooks = SerializeHelper::serialize($books);

Put your four lines of code in there and you're good to go.

The advantage is, that I (as Doctrine lead developer) don't have to maintain XML/JSON/Everything Encoding and potential bugs and people requesting new features, but can crowdsource this to others. We do have enough to work on Doctrine 2 new features and improvements and are not responsible to release code that has nothing to do with Object Relational Mapping whatsoever. Serialization has tones of different approaches, why should Doctrine force some way on developers?

What I don't want is to release some hacked code just to have serialization as a feature and feel the maintenance pain down the road and disappoint users by making them false promises.

@fzaninotto
Copy link
Author

I get your point. So the problem probably doesn't lie in Doctrine2 but rather in Symfony2. If Doctrine2 is only an ORM, Symfony2 is a Framework. I'd expect from a framework to make common tasks easier - and providing the kind of helper you mention is part of that.

And if the Symfony2 guys think like you and don't want to commit to maintain code that handles serialization in a usable way, that means there is a hole.

For the end user like me, it makes no difference if the problem is in Doctrine2 or in Symfony2. I see only useless verbosity.

@kriswallsmith
Copy link

If you're going to use DI you need a configuration layer, which the DIC provides. In this gist you're misusing the DIC by injecting it instead of your actual dependencies. With proper DI this becomes...

$xmlBooks = $serializer->serialize($bookRepo->findAll(), 'xml');

@fzaninotto
Copy link
Author

If I understand you correctly, I have to define some configuration for 3 service classes in a separate file just to do something like '->toXml()'? It's probably better in the controller class, but it's still very verbose overall.

Would you have an example of the DIC configuration it requires?

@kriswallsmith
Copy link

Yes, if you're following DI, that's how I would do it.

my_controller:
    class: MyController
    arguments:
        - @book_repo
        - @serializer

@fzaninotto
Copy link
Author

But the serializer needs to be defined as well in the DIC, doesn't it?

@kriswallsmith
Copy link

Yes, I'm not familiar with the Serializer component, but the configuration would look similar.

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