leonardorcortes
Can you post example using annotation to map VO? Can I use annotation to map services too?
#4 · open · 4 comments
Can you post example using annotation to map VO? Can I use annotation to map services too?
Hey. the first thing you need is my fork of the JMSSerializerBundle, you found this here: https://github.com/tecbot/JMSSerializerBundle then define class mappings in the config: ``` tecbot_amf: use_serialization: true # activate this for vo serialization mappings: FooVO: My\Bundle\Foo ``` and the last step. Define the serialization mapping for the JMSSerializerBundle: ``` <?xml version="1.0" encoding="UTF-8" ?> <serializer> <class name="My\Bundle\Foo" vo-class="FooVO" /> </serializer> ``` for the serialization stuff you can found more here: https://github.com/schmittjoh/JMSSerializerBundle Regards, Thomas
Serializer does not work with getter and setter: config.yml ``` yaml tecbot_amf: use_serialization: true services: ExampleAmfService: MyExampleBundle:ExampleAmf mappings: Example: My\ExampleBundle\Entity\Example ``` My\ExampleBundle\Resources\config\serializer\Example.xml ``` xml <serializer> <class name="My\ExampleBundle\Entity\Example" vo-class="Example" access-type="public_method" /> </serializer> ``` My\ExampleBunde\Entity\Example ``` php <?php namespace My\ExampleBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * Example * * @ORM\Table(name="example") * @ORM\Entity */ class Example { /** * @var bigint $id * * @ORM\Column(name="id", type="bigint", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="SEQUENCE") * @ORM\SequenceGenerator(sequenceName="seq_example", allocationSize=1, initialValue=1) */ private $id; /** * @var string $name * * @ORM\Column(name="name", type="string", length=100, nullable=false, unique=true) */ private $name; public function getId() { return $this->id; } public function setId($id) { $this->id = $id; } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } } ``` My\ExampleBundle\Amf\ExampleAmfService.php ``` php namespace My\ExampleBundle\Amf; use Symfony\Component\DependencyInjection\ContainerAware; class ExampleAmfService extends ContainerAware { public function getByIdAction($id) { $entity = $this->container->get('my.example_bundle.example_service')->getById($id); return $entity; } } ``` My\ExampleBundle\Service\ExampleService.php ``` php namespace My\ExampleBundle\Service; class ExampleService { private $entityManager; public function getEntityManager() { return $this->entityManager; } public function setEntityManager($entityManager) { $this->entityManager = $entityManager; } public function getById($id) { return $this->getEntityManager()->getRepository('My\ExampleBundle\Entity\Example')->find($id); } } ``` ## My\ExampleBundle\Resources\config\services.xml ``` xml <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> <services> <service id="my.example_bundle.example_service" class="My\ExampleBundle\Service\ExampleService"> <call method="setEntityManager"> <argument type="service" id="doctrine.orm.entity_manager" /> </call> </service> </services> </container> ``` Flex get object serialized correctly, but with null values using getter. Modifying entity atributes to public works. What can I do to use getter/setter?
You need to serialize the entity before you return. ``` namespace My\ExampleBundle\Amf; use Symfony\Component\DependencyInjection\ContainerAware; class ExampleAmfService extends ContainerAware { public function getByIdAction($id) { $entity = $this->container->get('my.example_bundle.example_service')->getById($id); return $this->container->get('serializer')->serialize($entity, 'vo'); } } ```
Ok, at this point, PHP and FLEX can understand types, but related entitites not. For example, City -> State: PHP: all properties from city entity are populated. FLEX: related properties (i.e: state) are null. What can I do to solve this?