2012-08-23 4 views
0

다음 항목이 있습니다. - 저장 및 패키지일대일 관계

패키지 ID를 추가하는 StoreType 양식을 작성하고 싶습니다.

나는 스토어 타입의 폼 클래스를했습니다

: 나는 1-1을 통해 패키지를 추가했습니다

(나는 모든 패키지가 나열되어이에서 선택 상자를 얻을 기대),하지만 내 문제가 저장소 양식에 패키지를 추가하면 양식이 작성되지만 데이터베이스 저장소 테이블 열 패키지에 empty.i가있는 이유는 무엇입니까?

  • Store.php
  • /** 
    * @ORM\Entity 
    */ 
    class Store 
        { 
         /** 
         * @ORM\Id 
         * @ORM\Column(type="integer") 
         * @ORM\GeneratedValue(strategy="AUTO") 
         */ 
         protected $id; 
    
         /** 
         * @ORM\Column(type="string", length="255") 
         */ 
         protected $title; 
    
         /** 
         * @ORM\Column(type="string", length="255") 
         */ 
         protected $domain; 
    
         /** 
         * @ORM\OneToOne(targetEntity="Package",cascade={"persist", "remove"}) 
         * @ORM\JoinColumn(name="package_id", referencedColumnName="id") 
         */ 
         protected $package; 
        } 
    

  • Package.php
  • /** 
    * @ORM\Entity 
    */ 
    class Package 
        { 
         /** 
          * @ORM\Id 
          * @ORM\Column(type="integer") 
          * @ORM\GeneratedValue(strategy="AUTO") 
          */ 
         protected $id; 
    
         /** 
          * @ORM\Column(type="string", length=255) 
          */ 
         protected $title; 
    
         /** 
          * @ORM\Column(type="text", length="4000") 
          */ 
         protected $description; 
    
         /** 
          * @ORM\Column(type="boolean") 
          */ 
         protected $active; 
    
    
         public function __toString() 
          { 
          return $this->getTitle(); 
          } 
        } 
    

  • StoreType.php
  • class StoreType extends AbstractType 
        { 
         public function buildForm(FormBuilder $builder, array $options) 
         { 
          $builder 
           ->add('title') 
           ->add('domain') 
           ->add('package','entity',array(
            'class' => 'WebmuchProductBundle:Package', 
          )); 
         } 
    
    
         public function getName() 
         { 
          return 'webmuch_productbundle_storetype'; 
         } 
        } 
    

  • 스토어 Controller.php
  • /** 
    * Store controller. 
    * 
    * @Route("/store") 
    */ 
    class StoreController extends Controller 
    { 
    /** 
    * Lists all Store entities. 
    * 
    * @Route("/", name="store") 
    * @Template() 
    */ 
    public function indexAction() 
    { 
        $em = $this->getDoctrine()->getEntityManager(); 
    
        $entities = $em->getRepository('WebmuchProductBundle:Store')->findAll(); 
    
        return array('entities' => $entities); 
    } 
    
    /** 
    * Finds and displays a Store entity. 
    * 
    * @Route("/{id}/show", name="store_show") 
    * @Template() 
    */ 
    public function showAction($id) 
    { 
        $em = $this->getDoctrine()->getEntityManager(); 
    
        $entity = $em->getRepository('WebmuchProductBundle:Store')->find($id); 
    
        if (!$entity) { 
         throw $this->createNotFoundException('Unable to find Store entity.'); 
        } 
    
        $deleteForm = $this->createDeleteForm($id); 
    
        return array(
         'entity'  => $entity, 
         'delete_form' => $deleteForm->createView(),  ); 
    } 
    
    /** 
    * Displays a form to create a new Store entity. 
    * 
    * @Route("/new", name="store_new") 
    * @Template() 
    */ 
    public function newAction() 
    { 
        $store = new Store(); 
        $form = $this->createForm(new StoreType(), $store); 
    
        return array(
         'entity' => $store, 
         'form' => $form->createView() 
        ); 
    } 
    
    /** 
    * Creates a new Store entity. 
    * 
    * @Route("/create", name="store_create") 
    * @Method("post") 
    * @Template("WebmuchProductBundle:Store:new.html.twig") 
    */ 
    public function createAction() 
    { 
        $entity = new Store(); 
        $request = $this->getRequest(); 
        $form = $this->createForm(new StoreType(), $entity); 
        $form->bindRequest($request); 
    
        if ($form->isValid()) { 
         $em = $this->getDoctrine()->getEntityManager(); 
         $em->persist($entity); 
         $em->flush(); 
    
         return $this->redirect($this->generateUrl('store_show', array('id' => $entity->getId()))); 
    
        } 
    
        return array(
         'entity' => $entity, 
         'form' => $form->createView() 
        ); 
    } 
    
    /** 
    * Displays a form to edit an existing Store entity. 
    * 
    * @Route("/{id}/edit", name="store_edit") 
    * @Template() 
    */ 
    public function editAction($id) 
    { 
        $em = $this->getDoctrine()->getEntityManager(); 
    
        $entity = $em->getRepository('WebmuchProductBundle:Store')->find($id); 
    
        if (!$entity) { 
         throw $this->createNotFoundException('Unable to find Store entity.'); 
        } 
    
        $editForm = $this->createForm(new StoreType(), $entity); 
        $deleteForm = $this->createDeleteForm($id); 
    
        return array(
         'entity'  => $entity, 
         'edit_form' => $editForm->createView(), 
         'delete_form' => $deleteForm->createView(), 
        ); 
    } 
    
    /** 
    * Edits an existing Store entity. 
    * 
    * @Route("/{id}/update", name="store_update") 
    * @Method("post") 
    * @Template("WebmuchProductBundle:Store:edit.html.twig") 
    */ 
    public function updateAction($id) 
    { 
        $em = $this->getDoctrine()->getEntityManager(); 
    
        $entity = $em->getRepository('WebmuchProductBundle:Store')->find($id); 
    
        if (!$entity) { 
         throw $this->createNotFoundException('Unable to find Store entity.'); 
        } 
    
        $editForm = $this->createForm(new StoreType(), $entity); 
        $deleteForm = $this->createDeleteForm($id); 
    
        $request = $this->getRequest(); 
    
        $editForm->bindRequest($request); 
    
        if ($editForm->isValid()) { 
         $em->persist($entity); 
         $em->flush(); 
    
         return $this->redirect($this->generateUrl('store_edit', array('id' => $id))); 
        } 
    
        return array(
         'entity'  => $entity, 
         'edit_form' => $editForm->createView(), 
         'delete_form' => $deleteForm->createView(), 
        ); 
    } 
    
    /** 
    * Deletes a Store entity. 
    * 
    * @Route("/{id}/delete", name="store_delete") 
    * @Method("post") 
    */ 
    public function deleteAction($id) 
    { 
        $form = $this->createDeleteForm($id); 
        $request = $this->getRequest(); 
    
        $form->bindRequest($request); 
    
        if ($form->isValid()) { 
         $em = $this->getDoctrine()->getEntityManager(); 
         $entity = $em->getRepository('WebmuchProductBundle:Store')->find($id); 
    
         if (!$entity) { 
          throw $this->createNotFoundException('Unable to find Store entity.'); 
         } 
    
         $em->remove($entity); 
         $em->flush(); 
        } 
    
        return $this->redirect($this->generateUrl('store')); 
    } 
    
    private function createDeleteForm($id) 
    { 
        return $this->createFormBuilder(array('id' => $id)) 
         ->add('id', 'hidden') 
         ->getForm() 
        ; 
    } 
    } 
    
    +0

    귀하의 코드가 잘 될 것 같다. 심지어 코드를 사용했는데 모든 것이 괜찮습니다. 차이점은 필자가 getters/setter를 만드는 것을 피하기 위해 모든 속성에 공개 액세스했습니다.하지만 getters/setter가 있으면 잘 작동해야 함). 그래서 저는 여러분에게 엔티티와 네임 스페이스에서 세터/게터를 확인해보십시오. – Cyprian

    +0

    plz SQL 서버 태그 –

    답변

    0

    나는 당신이 당신의 createAction에서이 작업을 수행하는 것을 잊지 생각 :

    $request = $this->getRequest(); 
    $form->setData($request->getPost()); 
    
    +0

    제거 : $ form-> bindRequest ($ request); "setData" – Cyprian

    +0

    을 필요로하지 않으므로 $ request-> getPost() 및 $ form-> getData()에 대해 var_dump()를 참조해야합니다. –

    +1

    Cyprian 및 Sina Miandashti 감사합니다. 해결했습니다. 엔터티를 공개로 저장하십시오. 공공 기능 setId() {return $ this-> id; } – Sid