符号2一对多编辑操作


Symfony2 One-to-many edit action

Symfony2中的mu一对多关系有问题。我有公司类和用户类。一家公司有一些用户作为人力资源,一些用户有公司雇主。有部分代码:

 /**
 * @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 %}

一切正常,除了添加或编辑人力资源列表。谢谢你的帮助!

我解决了一个问题。

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(),
    ));
}

它可能对某些人有用,所以我把它留在那里:)