嗨,大家好我試圖讓FloorType窗體有一個隱藏的對象域(我正在建設),我不能轉換對象實體我有使用轉換代碼從食譜。Symfony2數據轉換器不能轉換
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();
讓我明白了變壓器失敗,但我看不到的地方......有人可以幫我這個轉變?
不,這是不是這....現在一切工作正常我不明白這是什麼.... –
它可能似乎工作,但除非你使用上面的語法,然後模型轉換器被錯誤地應用。如果添加了另一個字段,或者訂單更改或存在月食,則可能會遇到麻煩。相信文檔。 – Cerad