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%"]