Por favor, necesito ayuda con esto.
código en línea
<?php namespace AppBundle\Form\DataTransformer; use AppBundle\Entity\Content; use Doctrine\Common\Persistence\ObjectManager; use Symfony\Component\Form\DataTransformerInterface; use Symfony\Component\Form\Exception\TransformationFailedException; class ContentToNumberTransformer implements DataTransformerInterface { private $om; public function __construc(ObjectManager $om){ $this->om = $om; } /** * Transforma un object Content a un string: su id. * * @param Content|null $content * @return string */ public function transform($content){ if (null === $content) { return ''; } return $content->getId(); } /** * Transforma un string (id) en un objecto Content. * * @param string $id * @return Content|null * @throws TransformationFailedException if object (Content) is not found. */ public function reverseTransform($id){ if (!$id) { return; } die(var_dump($this->om)); // AQUI OBTENGO NULL $content = $this->om ->getRepository("AppBundle:Content")->find($id); if (null === $content) { throw new TransformationFailedException(sprintf( 'El Contenido con id "%s" no existe!', $id )); } return $content; } }
<?php namespace AppBundle\Form; use AppBundle\Form\DataTransformer\ContentToNumberTransformer; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Doctrine\Common\Persistence\ObjectManager; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\TextType; class RelatedType extends AbstractType { private $om; public function __construct(ObjectManager $om){ $this->om = $om; } public function buildForm(FormBuilderInterface $builder, array $options){ $builder ->add('id',HiddenType::class,array('invalid_message' => 'El Contenido relacionado no es válido.')) ->add('title',TextType::class,array('mapped'=>false,'label'=>false)); $builder->get('id')->addModelTransformer(new ContentToNumberTransformer($this->om)); } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'compound' => true, )); } public function getParent(){ return HiddenType::class; } public function getName(){ return 'related'; } }
services.yml
services: app.form.type.related: class: AppBundle\Form\RelatedType arguments: ['@doctrine.orm.entity_manager'] tags: - { name: form.type }
Respuestas
Hola @espantaperros_
Revisa el método construc debe ser contruct()
y otra cosa es que en el servicio estás inyectando el EntityManager pero el constructor esta recibiendo el ObjectManager
Saludos.
@miguelplazasr
27 julio 2017, 17:32