0
votes

I use Objectmanger for use Doctrine .. I create instance of objectManager and I create service but I have this bug:

Catchable Fatal Error: Argument 1 passed to Tag\TagBundle\Form\Types\TagsType::__construct() must be an instance of Doctrine\Common\Persistance\ObjectManager, instance of Doctrine\ORM\EntityManager given, called in /var/www/html/TagProject/var/cache/dev/appDevDebugProjectContainer.php on line 2412 and defined

code TagsType:

<?php 

    namespace Tag\TagBundle\Form\Types;

    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\Extension\Core\Type\TextType;
    use Tag\TagBundle\Form\DataTransformer\TagsTransformer;
    use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
    use Symfony\Component\Form\FormBuilderInterface;
    use Doctrine\Common\Persistance\ObjectManager;

    class TagsType extends AbstractType {

    /**
     * @var ObjectManager
     */

    private $manager;

    public function __construct(ObjectManager $manager){
        $this->manager = $manager;
    }

        public function buildForm(FormBuilderInterface $builder, array $options){
            $builder
                    ->addModelTransformer(new CollectionToArrayTransformer(),true)
                    ->addModelTransformer(new TagsTransformer($this->manager),true);
        }

        public function getParent(){
            return TextType::class;
        }

    }

code TagsTransformer:

<?php 

            namespace Tag\TagBundle\Form\DataTransformer;

            use Symfony\Component\Form\DataTransformerInterface;
            use Tag\TagBundle\Entity\Tag;
            use Doctrine\Common\Persistance\ObjectManager;

            class TagsTransformer implements DataTransformerInterface {


            /**
             * @var ObjectManager
             */

            private $manager;

            public function __construct(ObjectManager $manager){
                $this->manager = $manager;
            }

            public function transform($value) {
                dump($value);
                return implode(',',$value);
            }

                public function reverseTransform($string)
                {
                    $names = explode(',',$string);
                    $tags = $this->manager->getRepository('TagBundle:Tag')->findBy(['name' => $names]);
                    $newNames = array_diff($names, $tags);
                    foreach($names as $name){
                        $tag = new tag();
                        $tag->setName($name);
                        $tags[] = $tag;
                    }
                    return $tags;
                }
            }

code services.yml:

services:
  tag.form.types.tages:
    class: Tag\TagBundle\Form\Types\TagsType
    arguments: ["@doctrine.orm.entity_manager"]
    tags:
      - { name: form.type }
#    tag.example:
#        class: Tag\TagBundle\Example
#        arguments: ["@service_id", "plain_value", "%parameter%"]
1
It is a bit puzzling. Might try the ever popular cache clearing. And if the only reason you are injecting the entity manager is to pass it on to the transformer then you might consider defining the transformer as a service and injecting it. But again, I routinely use ObjectManager as a type hint and it works for me.Cerad
I finally got around to testing this on a clean S33 app and ObjectManager still works just fine for me. I think your error message might be coming from someplace else.Cerad

1 Answers

2
votes

You're injecting an instance of EntityManager so you should use the corresponding interface Doctrine\ORM\EntityManagerInterface for typehinting.

use Doctrine\ORM\EntityManagerInterface;

class TagsTransformer implements DataTransformerInterface 
{
    /**
     * @var EntityManagerInterface
     */
    private $manager;

    public function __construct(EntityManagerInterface $manager)
    {
        $this->manager = $manager;
    }