Created
March 9, 2011 15:16
-
-
Save fzaninotto/862369 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
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?
Yes, if you're following DI, that's how I would do it.
my_controller:
class: MyController
arguments:
- @book_repo
- @serializer
But the serializer needs to be defined as well in the DIC, doesn't it?
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
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...