2012-11-06 2 views
0

"sonata_type_collection"유형의 포함 된 양식 수를 제한하는 방법은 무엇입니까? 나는 마지막 다섯 전화에 제한을하고자소나타 관리 양식 모음

$formMapper->add('phones', 'sonata_type_collection', 
         array(
          'required' => true, 
          'by_reference' => false, 
          'label' => 'Phones', 
          ), 
         array(
          'edit' => 'inline', 
          'inline' => 'table' 
          ) 

, 나는 "edit_orm_one_to_many"템플릿 나뭇 가지에 표시를 제한, 지금 만이 솔루션을 발견,하지만 난 그렇게하지 않습니다.

class ContactAdminController extends Controller 
{ 

    public function editAction($id = null) 
    { 
     // the key used to lookup the template 
     $templateKey = 'edit'; 

     $em = $this->getDoctrine()->getEntityManager(); 
     $id = $this->get('request')->get($this->admin->getIdParameter()); 

     // $object = $this->admin->getObject($id); 
     // My custom method to reduce the queries number 
     $object = $em->getRepository('GestionBundle:Contact')->findOneAllJoin($id); 

     if (!$object) 
     { 
      throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id)); 
     } 

     if (false === $this->admin->isGranted('EDIT', $object)) 
     { 
      throw new AccessDeniedException(); 
     } 

     $this->admin->setSubject($object); 

     /** @var $form \Symfony\Component\Form\Form */ 
     $form = $this->admin->getForm(); 
     $form->setData($object); 

     // Trick is here ############################################### 
     // Method to find the X last phones for this Contact (x = limit) 
     // And set the data in form 
     $phones = $em->getRepository('GestionBundle:Phone')->findLastByContact($object, 5); 
     $form['phones']->setData($phones); 
     // ############################################################# 

     if ($this->get('request')->getMethod() == 'POST') 
     { 
      $form->bindRequest($this->get('request')); 

      $isFormValid = $form->isValid(); 

      // persist if the form was valid and if in preview mode the preview was approved 
      if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) 
      { 
       $this->admin->update($object); 
       $this->get('session')->setFlash('sonata_flash_success', 'flash_edit_success'); 

       if ($this->isXmlHttpRequest()) 
       { 
        return $this->renderJson(array(
         'result' => 'ok', 
         'objectId' => $this->admin->getNormalizedIdentifier($object) 
        )); 
       } 

       // redirect to edit mode 
       return $this->redirectTo($object); 
      } 

      // show an error message if the form failed validation 
      if (!$isFormValid) 
      { 
       $this->get('session')->setFlash('sonata_flash_error', 'flash_edit_error'); 
      } 
      elseif ($this->isPreviewRequested()) 
      { 
       // enable the preview template if the form was valid and preview was requested 
       $templateKey = 'preview'; 
      } 
     } 

     $view = $form->createView(); 

     // set the theme for the current Admin Form 
     $this->get('twig')->getExtension('form')->renderer->setTheme($view, $this->admin->getFormTheme()); 

     return $this->render($this->admin->getTemplate($templateKey), array(
      'action' => 'edit', 
      'form' => $view, 
      'object' => $object, 
     )); 
    } 
} 
:

답변

0

나는 는 문서 sonataAdminBundle 이러한 내 관리 컨트롤러 클래스를 생성, 컨트롤러에서 편집 작업을 다시 작성하여 해결책을 발견
관련 문제