Symfony Doctrine多对多插入CollectionType

时间:2016-04-27 15:56:22

标签: php doctrine-orm doctrine symfony

我在Symfony中遇到了我的实体和控制器的问题。我会在生成的多对多表中插入我的DB值。

仅包含多对多元素的实体请求

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

insert的Form类型是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(),
    ));
}

当我把die用于返回$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.

1 个答案:

答案 0 :(得分:1)

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

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
}

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