大家好我正在尝试制作一个FloorType表单,其中有一个隐藏的对象字段(我正在构建),我无法转换对象实体,我已经使用了食谱中的转换代码。
http://symfony.com/doc/2.7/cookbook/form/data_transformers.html
以下是代码:
变压器
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;
}
}
场地类型
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')->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';
}
}
CreateCreteForm
private function createCreateForm(Floor $entity)
{
$manager = $this->getDoctrine()->getManager();
$form = $this->createForm(new FloorType($manager), $entity, array(
'action' => $this->generateUrl('admin_floor_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
服务
<services>
<service id="app.form.type.task" class="George\FloorBundle\Form\FloorType">
<tag name="form.type" />
<argument type="service" id="doctrine.orm.entity_manager"></argument>
</service>
</services>
最大的错误是:
Catchable Fatal Error: Object of class George\ObjectsBundle\Entity\Object could not be converted to string
有趣的是,该服务不起作用我需要在管理器的创建形式中输入:
$manager = $this->getDoctrine()->getManager();
所以我明白变压器发生故障但是我看不到哪里......有人可以帮我解决这个转变吗?
答案 0 :(得分:1)
你的语法有点偏。 $ builder-&gt; add实际返回一个构建器对象,而不是添加的表单对象。这本书显示:
$builder->add(
$builder->create('description', 'textarea')
->addModelTransformer(...)
);
在你的情况下,它会是这样的:
$builder
->add('translations', 'a2lix_translations',array(
'required_locales' => array('bg','en')
))
->add(
$builder->create('object','text')
->addModelTransformer(new ObjectToNumberTransformer($this->manager))
));
可能让parens搞砸了,但你明白了。
答案 1 :(得分:1)
你应该使用这个
$builder
->add('translations', 'a2lix_translations',array(
'required_locales' => array('bg','en')
))
->add('object');
$builder->get('object')->addModelTransformer(new ObjectToNumberTransformer($this->manager));
而不是
$builder
->add('translations', 'a2lix_translations',array(
'required_locales' => array('bg','en')
))
->add('object')->addModelTransformer(new ObjectToNumberTransformer($this->manager));
希望它有所帮助!!
答案 2 :(得分:0)
关于第二个问题,解决方案是在服务中使用别名:
<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',
));