Symfony Doctrine多对多插入

时间:2016-04-27 09:26:27

标签: php entity-framework symfony doctrine-orm doctrine

我在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类型是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(),
    ));
}

当我把die换回$form["tipi"]->getData()的值时,我得到一个数组集合:

Doctrine\Common\Collections\ArrayCollection@000000005b52ae6b00000000731dd0b4

但是我收到了这个错误

  

捕获致命错误:传递给AppBundle \ Entity \ Requests :: addTipi()的参数1必须是AppBundle \ Entity \ Tipi的实例,Doctrine \ Common \ Collections \ ArrayCollection的实例,在C:\ xampp \中调用第31行的htdocs \ bugaro \ src \ AppBundle \ Controller \ RequestsController.php并定义了

1 个答案:

答案 0 :(得分:0)

Request::addTipi()功能将单个技巧添加到其内部集合中。因此,您无法在通话中添加整个ArrayCollection

您有两种选择。

执行foreach

foreach($form["tipi"]->getData() as $tipi) {
    $requests->addTipi($tipi);
}

创建类似Requests::addCollections(ArrayCollection[])

的多分词符号
public function addTipis($tipis) {

    foreach($tipis as $tipi) {
        $this->tipi[] = $tipi;
    }

    return $this;
}

无论如何,你的代码中还有很多东西要修复。这里的帖子太多了。

但最重要的修复是您不需要大部分控制器代码。 ; - )

由于您将$requests对象传递到表单中,因此它已经被双向绑定,这意味着Symfony的表单组件应该使用新值自动填充它的属性。这包括许多关系的集合。

此外,如果您不传递该对象,$form->getData()应该返回一个新的Resnponses实例,因此您不需要创建它并手动传递,直到它是现有实例的版本。