这是FormType FloorType
inotify-tools
需要注入经理的服务
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>
我需要加载FloorType的控制器方法:
<?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;
}
}
所以当我删除经理时:
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;
}
我收到了一个很大的错误:
捕获致命错误:传递给George \ FloorBundle \ Form \ FloorType :: __ construct()的参数1必须实现接口Doctrine \ Common \ Persistence \ ObjectManager,没有给定,在D:\ work \ infinity3 \ src \ George \中调用第171行的FloorBundle \ Controller \ FloorController.php并定义了
我理解为这样 - 服务没有设法注入经理
在本地捆绑中调试服务
我已调试该服务并列出但我无法管理它以将Floor中的经理包含在这里我错过了什么?
修改
所以这是服务xml问题我没有包含别名属性感谢@Matteo回答我编辑代码它就像一个魅力! 谢谢@Matteo!
以下是修改服务
$manager = $this->getDoctrine()->getManager();
我在哪里创建表单:
<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>
答案 0 :(得分:1)
您应该在tags部分中定义别名,如下所示:
service.xml中
<services>
<service id="app.form.type.floor" class="George\FloorBundle\Form\Type\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',
));
希望这个帮助