2014-05-10 7 views
5

में छिपा हुआ इकाई फ़ील्ड प्रकार मैं नियंत्रक में अपनी विदेशी कुंजी इकाई के लिए एक छिपी हुई मूल्य बनाना चाहता हूं।सिम्फनी 2

मेरे पिछले नियंत्रक की तरह इस (ठीक काम करता है) है:

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

मैं इस तरह मेरी फार्म के लिए एक छिपे हुए मान देना चाहते हैं:

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

लेकिन यह मुझे एक त्रुटि देता है:

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

मैं एक विदेशी कुंजी इकाई को मूल्य कैसे निर्दिष्ट कर सकता हूं जो छुपा हुआ है? बहुत बहुत धन्यवाद।

+0

डुप्लिकेट: http://stackoverflow.com/questions/16905490/symfony2-data-transformer-on-hidden-field – ihsan

उत्तर

1

Yohooo, अंत में अपने काम करता है ... मैं फॉर्म बनाने से पहले इकाई डिफ़ॉल्ट परिभाषित की जरूरत है और 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(),    
)); 
} 

आशा कोई है जो इस समस्या में भी संघर्ष haha ​​मदद कर सकते हैं ...