2016-03-23 159 views
0

我在Symfony2中遇到了mu一對多關係問題。 我有公司和用戶類。一家公司擁有少數用戶作爲人力資源,一些用戶擁有公司僱主。 有部分代碼:Symfony2一對多編輯動作

/** 
* @ORM\Entity 
* @ORM\Table(name="fos_user") 
*/ 
class User extends BaseUser 
{ 
/** 
* @ORM\Id 
* @ORM\Column(type="integer") 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** 
* @ORM\ManyToOne(targetEntity="Company", inversedBy="hrs") 
* @ORM\JoinColumn(name="company_id", referencedColumnName="id") 
*/ 
protected $employer; 
.... 
.... 
.... 
    /** 
    * Set employer 
    * 
    * @param \JobFyBundle\Entity\Company $employer 
    * @return User 
    */ 
    public function setEmployer(\JobFyBundle\Entity\Company $employer = null) 
    { 
     $this->employer = $employer; 
     $employer->addHrs($this); 

     return $this; 
    } 

    /** 
    * Get employer 
    * 
    * @return \JobFyBundle\Entity\Company 
    */ 
    public function getEmployer() 
    { 
     return $this->employer; 
    } 
} 

這是公司級的一部分

/** 
* @ORM\Entity(repositoryClass="CompanyRepository") 
* @ORM\Table(name="jobfy_company") 
*/ 
class Company { 
    /** 
    * @ORM\Id() 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 
... 
... 
... 
/** 
    * Add hrs 
    * 
    * @param User $hr 
    * @return Company 
    * @internal param User $hrs 
    */ 
    public function addHrs(\JobFyBundle\Entity\User $hr) 
    { 
     $this->getHrs()->add($hr); 
     $hr->setEmployer($this); 

     return $this; 
    } 

    /** 
    * Remove hrs 
    * 
    * @param \JobFyBundle\Entity\User $hrs 
    */ 
    public function removeHr(\JobFyBundle\Entity\User $hrs) 
    { 
     $this->hrs->removeElement($hrs); 
    } 

    /** 
    * Get hrs 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getHrs() 
    { 
     return $this->hrs; 
    } 
} 

我儘量讓編輯操作,允許添加更多的人力資源的。

這裏是CompanyController的一部分:

/** 
* Displays a form to edit an existing Company entity. 
* 
* @Route("/{id}/edit", name="company_edit") 
* @Method({"GET", "POST"}) 
*/ 
public function editAction(Request $request, Company $company) 
{ 
    $deleteForm = $this->createDeleteForm($company); 
    $editForm = $this->createForm('JobFyBundle\Form\CompanyType', $company, array('csrf_protection' => false)); 
    $editForm->handleRequest($request); 

    $em = $this->getDoctrine()->getManager(); 
    $people = $em->getRepository('JobFyBundle:User')->findAll(); 

    if ($editForm->isSubmitted() && $editForm->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($company); 
     $em->flush(); 

     return $this->redirectToRoute('company_show', array('id' => $company->getId())); 
    } 

    return $this->render('company/edit.html.twig', array(
     'company' => $company, 
     'hrs' => $people, 
     'edit_form' => $editForm->createView(), 
     'delete_form' => $deleteForm->createView(), 
    )); 
} 

而且它的編輯操作的模板。

{% extends 'base.html.twig' %} 

{% block body %} 
    <div class="container"> 
     <form name="company" method="post" class="form-new"> 
      <h1 class="form-new-heading">Edit company</h1> 
      <div id="cv"> 
       <div> 
        <label for="company_title">Company title</label> 
        <textarea id="company_title" name="company[title]" class="form-control" 
           required autofocus>{{ company.title }}</textarea> 
        <label for="company_url">Company url</label> 
        <textarea id="company_url" name="company[url]" class="form-control" 
         >{{ company.url }}</textarea> 
        <label for="company_about_us">About us</label> 
        <textarea id="company_about_us" name="company[about_us]" 
           class="form-control" rows="15">{{ company.getAboutUs }}</textarea> 
        <div> 
         <label for="company_hrs">HRs</label> 
         <select id="company_hrs" name="company[hrs][]" multiple="multiple" class="form-control"> 
          {% for hr in hrs %} 
           <option value="{{ hr.id }}" 
             {% if hr.employer is not null and hr.employer.id == company.id %} 
              selected="selected" 
             {% endif %} 
           >{{ hr }}</option> 
          {% endfor %} 
         </select> 
        </div> 
        <button class="btn btn-lg btn-success" type="submit">Submit</button> 
       </div> 
      </div> 
     </form> 
     <h3><a href="{{ path('company_index_paginate') }}">Show companies list</a></h3> 
    </div> 
{% endblock %} 

一切工作,除了添加或編輯人力資源的列表。 謝謝你的幫助!

+0

'$ hrs'是[ArrayCollection的(http://doctrine-orm.readthedocs.org/projects/ doctrine-orm/en/latest/reference/association-mapping.html),你是否在'Company'實體的'__construct()'中設置了它? 'public function __construct(){$ this-> hrs = new ArrayCollection(); }' – Egg

+0

不,我沒有。我現在就設置好了,但不幸的是,它沒有幫助。但是謝謝你! –

回答

0

我已經解決了一個問題。

public function editAction(Request $request, Company $company) 
{ 
    $deleteForm = $this->createDeleteForm($company); 
    $editForm = $this->createForm('JobFyBundle\Form\CompanyType', $company, array('csrf_protection' => false)); 
    $editForm->handleRequest($request); 

    $em = $this->getDoctrine()->getManager(); 
    $people = $em->getRepository('JobFyBundle:User')->findAll(); 

    foreach ($company->getHrs() as $hr){ 
     $company->removeHr($hr); 
    } 

    if ($editForm->isSubmitted() && $editForm->isValid()) { 
     foreach ($_POST['company']['hrs'] as $hr_id){ 
      $hr = $em->getRepository('JobFyBundle:User')->find($hr_id); 
      $company->addHrs($hr); 
      $hr->setEmployer($company); 
     } 

     $em->persist($company); 
     $em->flush(); 
     return $this->redirectToRoute('company_show', array('id' => $company->getId())); 
    } 

    return $this->render('company/edit.html.twig', array(
     'company' => $company, 
     'hrs' => $people, 
     'edit_form' => $editForm->createView(), 
     'delete_form' => $deleteForm->createView(), 
    )); 
} 

它可以爲別人有用的,所以我會離開它那裏:)