【发布时间】:2015-12-03 09:04:53
【问题描述】:
伙计们,出于某种原因,我正在尝试将其包含在一种表单类型中,经理未将其包含在构造函数中,这可能是一个简单的错字,但现在我无法通过 symfony2 (2.7) 的食谱示例看到错误.
这是 FormType FloorType
namespace George\FloorBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use George\FloorBundle\Form\DataTransformer\ObjectToNumberTransformer;
use Doctrine\Common\Persistence\ObjectManager;
class FloorType extends AbstractType
{
private $manager;
public function __construct(ObjectManager $manager)
{
$this->manager = $manager;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('translations', 'a2lix_translations',array(
'required_locales' => array('bg','en')
))
->add('object', 'hidden', array(
// validation message if the data transformer fails
'invalid_message' => 'That is not a valid issue number',
));
$builder ->get('object')->addModelTransformer(new ObjectToNumberTransformer($this->manager));
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'George\FloorBundle\Entity\Floor'
));
}
/**
* @return string
*/
public function getName()
{
return 'george_floorbundle_floor';
}
}
需要注入管理器的服务
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="app.form.type.floor" class="George\FloorBundle\Form\Type\FloorType">
<tag name="form.type" />
<argument type="service" id="doctrine.orm.entity_manager"></argument>
</service>
</services>
使用管理器的转换器(但我没有错误,我只想做一个完整的案例)
<?php
namespace George\FloorBundle\Form\DataTransformer;
use George\ObjectsBundle\Entity\Object;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class ObjectToNumberTransformer implements DataTransformerInterface
{
private $manager;
public function __construct(ObjectManager $manager)
{
$this->manager = $manager;
}
/**
* Transforms an object (issue) to a string (number).
*
* @param Object|null $issue
* @return string
*/
public function transform($object)
{
if (null === $object) {
return '';
}
return $object->getId();
}
/**
* Transforms a string (number) to an object (issue).
*
* @return Object|null
* @throws TransformationFailedException if object (issue) is not found.
*/
public function reverseTransform($objectNumber)
{
// no issue number? It's optional, so that's ok
if (!$objectNumber) {
return;
}
$object= $this->manager
->getRepository('ObjectsBundle:Object')
// query for the issue with this id
->find($objectNumber)
;
if (null === $object) {
// causes a validation error
// this message is not shown to the user
// see the invalid_message option
throw new TransformationFailedException(sprintf(
'An issue with number "%s" does not exist!',
$objectNumber
));
}
return $object;
}
}
以及我需要加载 FloorType 的控制器方法:
private function createEditForm(Floor $entity)
{
$manager = $this->getDoctrine()->getManager();
$form = $this->createForm(new FloorType($manager), $entity, array(
'action' => $this->generateUrl('admin_floor_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
return $form;
}
所以当我删除经理时:
$manager = $this->getDoctrine()->getManager();
我遇到了一个大错误:
可捕获的致命错误:传递给 George\FloorBundle\Form\FloorType::__construct() 的参数 1 必须实现接口 Doctrine\Common\Persistence\ObjectManager,没有给出,在 D:\work\infinity3\src\George\ 中调用FloorBundle\Controller\FloorController.php 在第 171 行并定义了
我是这样理解的 - 服务没有设法注入管理器
在本地包中调试服务
我已经调试了服务并且它已列出,但我无法管理它以将管理器包含在 FloorType 中我在这里错过了什么?
编辑
所以这是服务 xml 问题,由于@Matteo 的回答,我没有包含别名属性,我编辑了代码,它就像一个魅力! 谢谢@Matteo!
这里是编辑服务
<services>
<service id="app.form.type.floor" class="George\FloorBundle\Form\FloorType">
<tag name="form.type" alias="george_floorbundle_floor" />
<argument type="service" id="doctrine.orm.entity_manager"></argument>
</service>
</services>
我在哪里创建表单:
$form = $this->createForm('george_floorbundle_floor', $entity, array(
'action' => $this->generateUrl('admin_floor_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
【问题讨论】:
标签: php symfony service doctrine-orm dependency-injection