我使用Objectmanger来使用Doctrine ..我创建了objectManager的实例,我创建了服务,但是我有这个bug:
捕获致命错误:传递给Tag \ TagBundle \ Form \ Types \ TagsType :: __ construct()的参数1必须是Doctrine \ Common \ Persistance \ ObjectManager的实例,Doctrine \ ORM \ EntityManager的实例,在/中调用第2412行的var / www / html / TagProject / var / cache / dev / appDevDebugProjectContainer.php并定义了
代码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;
}
}
代码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%"]
答案 0 :(得分:0)
您正在注入EntityManager
的实例,因此您应该使用相应的界面Doctrine\ORM\EntityManagerInterface
进行类型提示。
use Doctrine\ORM\EntityManagerInterface;
class TagsTransformer implements DataTransformerInterface
{
/**
* @var EntityManagerInterface
*/
private $manager;
public function __construct(EntityManagerInterface $manager)
{
$this->manager = $manager;
}