2014-05-10 11 views
5

Chcę wprowadzić ukrytą wartość dla mojego klucza obcego w kontrolerze.Podmiot Typ pola ukryty w Symfony2

Moje poprzednie regulator jest tak (działa poprawnie):

->add('id_grup', 'entity', array('class' => 'Sifo\AdminBundle\Entity\MstGrup')) 

chcę przypisać ukrytą wartość do mojej postaci takiego:

->add('id_grup', 'hidden', array('data' => $id)) 

Ale to daje mi błąd:

ContextErrorException: Catchable Fatal Error: Argument 1 passed to Sifo\AdminBundle\Entity\DftGrupMapel::setIdGrup() must be an instance of Sifo\AdminBundle\Entity\MstGrup, string given, called in C:\Sifony\vendor\symfony\symfony\src\Symfony\Component\PropertyAccess\PropertyAccessor.php on line 360 and defined in C:\Sifony\src\Sifo\AdminBundle\Entity\DftGrupMapel.php line 179

Jak mogę przypisać wartość ukrytemu kluczowi obcemu? Dziękuję bardzo.

+0

duplikat: http://stackoverflow.com/questions/16905490/symfony2-data-transformer-on-hidden-field – ihsan

Odpowiedz

1

Yohooo wreszcie jej działa ... muszę przed określić domyślne jednostki tworzyć formę i nie dodać ponownie w FormBuilder:

public function manageAction(Request $request, $id) 
{ 
    $em = $this->getDoctrine()->getManager(); 
$entity = $em->getRepository('SifoAdminBundle:MstGrup')->find($id); 

if (!$entity) { 
    throw $this->createNotFoundException('Unable to find MstGrup entity.'); 
} 

$entity_new = new DftGrupMapel(); 
$entity_new->setIdGrup($entity); 
$new_form = $this->createFormBuilder($entity_new) 
    ->setAction($this->generateUrl('admin_grup_mapel_manage', array('id' => $id))) 
    ->setMethod('POST') 
->getForm(); 

$new_form->handleRequest($request); 

if ($new_form->isValid()) { 
    $em_new = $this->getDoctrine()->getManager(); 
    $em_new->persist($entity_new); 
    $em_new->flush(); 

    return $this->redirect($this->generateUrl('admin_grup_mapel_manage', array('id' => $id))); 
} 

return $this->render('SifoAdminBundle:DftGrupMapel:manage.html.twig', array(
    'entity'  => $entity, 
    'new_form' => $new_form->createView(),    
)); 
} 

Nadzieja może pomóc komuś, którzy walczą z tym problemem zbyt haha ​​...

Powiązane problemy