Symfony主义多对多插入CollectionType

问题描述 投票:0回答:1

我在Symfony中的实体和控制器有问题。我会在生成的多对多表中插入数据库值。

仅具有许多元素的实体请求

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;
}
}

实体Tipi仅包含许多元素

 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;
}
 }

插入的表单类型是CollectionType

->add('tipi', CollectionType::class, array(
                'entry_type' => TipiType::class,
                'allow_add' => true,
                'prototype' => true,
                'entry_options' => array(
                    'required' => true,
                    'label' => false,
                )
            ))

并且TipiType是EntityType

->add('tipi', EntityType::class, array(
                'label' => 'Tipo',
                'class' => 'AppBundle:Tipi',
                'attr' => array('class' => 'form-control'),
                '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'));
        $em->persist($requests);
        $em->flush();
        return $this->redirectToRoute('requests_edit', array('id' => $requests->getId()));
    }
    return $this->render('AppBundle:Requests:index.html.twig', array(
                'requests' => $requests,
                'form' => $form->createView(),
    ));
}

当我为返回$form["tipi"]->getData()的值而死时,我得到一个数组集合:Doctrine\Common\Collections\ArrayCollection@000000005b52ae6b00000000731dd0b4

但是我收到此错误:

Expected value of type "Doctrine\Common\Collections\Collection|array" for association field "AppBundle\Entity\Requests#$tipi", got "AppBundle\Entity\Requests" instead.
php doctrine-orm doctrine symfony
1个回答
1
投票

您需要为此做一个学习,样本:

foreach $form->get("tipi")->getData() as $variableName) {
 //Here you just need the setter for each data from its own entity
->$manager
->setEntity(variableName)
// your entity tipi
}

然后从主事务或实体中将其清除后,它将自动插入集合中的所有数据。

© www.soinside.com 2019 - 2024. All rights reserved.