Symfonyのエンティティとコントローラに問題があります。私は、生成された多対多テーブルのDB値に挿入します。だけ多くの Symfony Doctrine多対多の挿入
多くの要素へclass Tipi {
/**
* @ORM\ManyToMany(targetEntity="Requests", mappedBy="tipi")
*/
private $requests;
/**
* Constructor
*/
public function __construct() {
$this->requests = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add request
*
* @param \AppBundle\Entity\Requests $request
*
* @return Tipi
*/
public function addRequest(\AppBundle\Entity\Requests $request)
{
$this->requests[] = $request;
return $this;
}
/**
* Remove request
*
* @param \AppBundle\Entity\Requests $request
*/
public function removeRequest(\AppBundle\Entity\Requests $request)
{
$this->requests->removeElement($request);
}
/**
* Get requests
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getRequests()
{
return $this->requests;
}
}
とだけ多くの
class Requests {
/**
* @ORM\ManyToMany(targetEntity="Tipi", inversedBy="requests")
* @ORM\JoinTable(name="tipi_richieste")
*/
private $tipi;
public function __construct() {
$this->tipi = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add tipi
*
* @param \AppBundle\Entity\Tipi $tipi
*
* @return Requests
*/
public function addTipi(\AppBundle\Entity\Tipi $tipi) {
$this->tipi[] = $tipi;
return $this;
}
/**
* Remove tipi
*
* @param \AppBundle\Entity\Tipi $tipi
*/
public function removeTipi(\AppBundle\Entity\Tipi $tipi) {
$this->tipi->removeElement($tipi);
}
/**
* Get tipi
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTipi() {
return $this->tipi;
}
}
多くの要素にエンティティティピーと
エンティティ・リクエストの挿入のためのフォームタイプは、EntityType
->add('tipi', EntityType::class, array(
'label' => 'Tipo',
'class' => 'AppBundle:Tipi',
'mapped' => false,
'attr' => array('class' => 'form-control'),
'multiple' => true,
'by_reference' => false,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('t');
},
))
されており、私のコントローラでは、私はこのように動作します:
public function indexAction(Request $request) {
$requests = new Requests();
$em = $this->getDoctrine()->getManager();
$form = $this->createForm(RequestsType::class, $requests);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$requests->setCreateAt(new \DateTime('now'));
$request_tipi = $form["tipi"]->getData();
$tipi_array = [];
die($form["tipi"]->getData());
$tipi_array = $em->getRepository('AppBundle:Tipi')->findOneBy(array('codice' => $form["tipi"]->getData()));
$tipi = new Tipi();
$requests->addTipi($form["tipi"]->getData());
$em->persist($requests);
$em->flush();
//return $this->redirectToRoute('immovable_edit', array('id' => $immovables->getId()));
}
return $this->render('AppBundle:Requests:index.html.twig', array(
'requests' => $requests,
'form' => $form->createView(),
));
}
私はリターンのために死ぬ置くとき$form["tipi"]->getData()
Iの値が配列のコレクションを取得:
Doctrine\Common\Collections\[email protected]
をしかし、私は
Catchable Fatal Error: Argument 1 passed to AppBundle\Entity\Requests::addTipi() must be an instance of AppBundle\Entity\Tipi, instance of Doctrine\Common\Collections\ArrayCollection given, called in C:\xampp\htdocs\bugaro\src\AppBundle\Controller\RequestsController.php on line 31 and defined
私は個人的に「EntityType」が1対多に最適であり、「CollectionType'」(https://symfony.com/doc/current/reference/forms/types/collection.html)あなたはすでにそれを試してみましたか? – kero
@kingkeroはい、うまくいきます。 –