在控制器symfony关系中保存信息ManyToMany HELPPPPP

时间:2017-12-05 13:22:37

标签: symfony controller doctrine

嗨我需要在我的数据库中保存信息,我的意思是,当关系很多时如何坚持,我这样做但是它不起作用!...我会放一些实体菜单的代码

     /** Agrega nuevo menù
         *
         * @Route("/save", name="admin_menu_save")
         * @Method({"GET", "POST"})
         * @param Request $request
         * @return Response
         */
        public function saveAction(Request $request)
        {
            $em = $this->getDoctrine()->getManager();
            $fecha_menu = $request->get('fecha');
            $fecha_comprar = $request->get('fechacomprado');
            $fecha_vencimiento = $request->get('fechavencimiento');
            $alimentos = $request->get('select_alimentos');
            $a = $em->getRepository('AdminBundle:Alimento')->findBy($alimentos);
            $menu = new Menu();
            $menu->setFecha(new \DateTime($fecha_menu));
            $menu->setFechacomprar(new \DateTime($fecha_comprar));
            $menu->setFechavence(new \DateTime($fecha_vencimiento));
            $menu->setPrecio(6);
            $menu->addAlimento($a);
            $em->persist($menu);
            $em->flush();
            return new Response('Guardado OK');
        }
 //Menu Entity Definition (Just the necessary code):

       /**
     * @ORM\ManyToMany(targetEntity="Alimento", inversedBy="menu")
     * @ORM\JoinTable(name="alimento_menu",
     *   joinColumns={
     *     @ORM\JoinColumn(name="menu_id", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="alimento_id", referencedColumnName="id")
     *   }
     * )
     */
    private $alimento;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->alimento = new ArrayCollection();
    }


    /**
     * Add alimento
     *
     * @param \AdminBundle\Entity\Alimento $alimento
     *
     * @return Menu
     */
    public function addAlimento(Alimento $alimento)
    {
        $this->alimento[] = $alimento;

        return $this;
    }

    /**
     * Remove alimento
     *
     * @param \AdminBundle\Entity\Alimento $alimento
     */
    public function removeAlimento(Alimento $alimento)
    {
        $this->alimento->removeElement($alimento);
    }


    /**
     * Get alimento
     *
     * @return ArrayCollection
     */
    public function getAlimento()
    {
        return $this->alimento;
    }

}

我没有经验处理多种关系,我希望解决这个问题,这显示非常好,但我不知道如何保存,编辑或删除多个表格!....

2 个答案:

答案 0 :(得分:1)

首先,这一点看起来很奇怪。

$alimentos = $request->get('select_alimentos');
$a = $em->getRepository('AdminBundle:Alimento')->findBy($alimentos);

Doctrine findBy采用一个数组,其中实体字段作为键,特定实体值作为值。像这样:

$em-getRepository('AdminBundle:Alimento')->findBy(['id' => $id]);

如果您的$alimentos变量的结构如此,那很好。它看起来很奇怪。

如果这是双向关系,则必须更新两个实体。所以你的控制器代码就像:

$a = $em->getRepository('AdminBundle:Alimento')->findBy($alimentos);
$menu = new Menu();

// -- more code ---//

$menu->addAlimento($a);
$a->addMenu($menu);
$em->persist($menu);
$em->flush();

Check this documentation

答案 1 :(得分:-1)

我刚才意识到......这是我的解决方案:

foreach ($alimentos as $item) {
            $a = $em->getRepository('AdminBundle:Alimento')->find($item);
            $menu->addAlimento($a);
        }

当然后来我坚持菜单。