0
Symfony2でmuの1対多の関係に問題があります。 私はCompanyクラスとUserクラスを持っています。 1つの会社にはHRとして少数のユーザーがおり、一部のユーザーには会社雇用者がいます。コードの一部は ありますSymfony2 1対多の編集アクション
/**
* @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;
}
}
それは私が編集アクションはより多くの時間のを追加できるように作ってみるCompanyクラス
/**
* @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 %}
hrのリストを追加または編集する以外はすべて動作します。 ありがとうございました!
'$の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
いいえ、私はしませんでした。私は今それを設定しましたが、残念ながら、それは助けにはなりませんでした。でもありがとう! –