У меня проблема с формами в Symfony2. Я пытаюсь укупить свою форму с заранее заполненными данными, но она всегда остается пустой.
public function updateAction($id,Request $request)
{
$contact = $this->getDoctrine()
->getManager()
->getRepository('HPContactBundle:Contact')
->findById($id);
$form = $this->createForm(new ContactType(), $contact);
return $this->render('HPContactBundle:Contact:update.html.twig',array(
'form' => $form->createView()
));
}
Ошибки:
The form view data is expected to be an instance of class HP\ContactBundle\Entity\Contact, but is a(n) array. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) array to an instance of HP\ContactBundle\Entity\Contact.
А ТАКЖЕ
at Form ->setData (array(object(Contact))) in vendor/symfony/symfony/src/Symfony/Component/Form/Form.php at line 488
я не знаю, чего я не хватает? У вас есть какая-то подсказка?
спасибо заранее.
findBy*()
возвращает массив сущностей. Используйте findOneById()
, который возвращает ровно одну сущность.