symfony - __construct() must be an instance of Doctrine\Common\Persistance\ObjectManager -
i use objectmanger use doctrine .. create instance of objectmanager , create service have bug:
catchable fatal error: argument 1 passed tag\tagbundle\form\types\tagstype::__construct() must 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 , 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 $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%"]
you're injecting instance of entitymanager
should use corresponding interface doctrine\orm\entitymanagerinterface
typehinting.
use doctrine\orm\entitymanagerinterface; class tagstransformer implements datatransformerinterface { /** * @var entitymanagerinterface */ private $manager; public function __construct(entitymanagerinterface $manager) { $this->manager = $manager; }
Comments
Post a Comment