【问题标题】:I'm struggling about a not working ManyToMany relationship in Symfony 2 with doctrine as ORM我正在为 Symfony 2 中的多对多关系与 ORM 学说不工作而苦苦挣扎
【发布时间】:2017-10-27 11:15:33
【问题描述】:

您好,提前感谢任何愿意提供帮助的人。

我对 Symfony 和学说还很陌生,我无法弄清楚为什么我的票证实体在没有 ManyToMany Realtionship 的情况下存储。 我非常努力,做了几天我发现的任何事情,但最后我需要帮助。

我们来了

门票

<pre>
    namespace AppBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;
    use Doctrine\Common\Collections\Collection;
    use AppBundle\Entity\Computer; 

    /**
     * @ORM\Entity
     * @ORM\Table(name="tickets")
     */
    class Ticket {

        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;

        /**
         * @ORM\Column(type="integer")
         */
        protected $creator;

        /**
         * @ORM\Column(type="integer")
         */
        protected $owner;

        /**
         * @ORM\ManyToOne(targetEntity="Category", inversedBy="tickets")
         * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
         */
        private $category;

        /**
         * @ORM\ManyToOne(targetEntity="Type", inversedBy="tickets")
         * @ORM\JoinColumn(name="type_id", referencedColumnName="id")
         */
        protected $type;

        /**
         * @ORM\Column(type="string")
         */
        protected $subject;

        /**
         * @ORM\ManyToMany(targetEntity="Computer", inversedBy="tickets", cascade={"persist"})
         * @ORM\JoinTable(name="inventory_computer_join",
         *      joinColumns={@ORM\JoinColumn(name="ticket_id", referencedColumnName="id")},
         *      inverseJoinColumns={@ORM\JoinColumn(name="computer_id", referencedColumnName="id")}
         *)
         */
        protected $computer;
    [...]
     /**
         * Add computer
         *
         * @param \AppBundle\Entity\Computer $computer
         *
         * @return Ticket
         */
        public function addComputer(\AppBundle\Entity\Computer $computer)
        {
            $this->computers[] = $computer;
            return $this;
        }

        /**
         * Remove computer
         * @param \AppBundle\Entity\Computer $computer
         */
        public function removeComputer(\AppBundle\Entity\Computer $computer)
        {
            $this->computers->removeElement($computer);
        }

        /**
         * Get computers
         * @return \Doctrine\Common\Collections\Collection
         */
        public function getComputers()
        {
            return $this->computers;
        }
    }
</pre>

电脑

<pre>
    namespace AppBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;
    use Doctrine\Common\Collections\Collection;
    use AppBundle\Entity\Ticket;

    /**
     * @ORM\Entity
     * @ORM\Table(name="inventory_computer")
     */
    class Computer
    {
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;

        /**
         * @ORM\ManyToMany(targetEntity="Ticket", mappedBy="computer")
         */
        protected $tickets;
        [...]
      /**
         * Add ticket
         *
         * @param \AppBundle\Entity\Ticket $ticket
         *
         * @return Computer
         */
        public function addTicket(\AppBundle\Entity\Ticket $ticket)
        {
            $this->tickets[] = $ticket;

            return $this;
        }
</pre>

控制器

<pre>
    /**
         * @Route("admint/createTicket", name="AdminTicketErstellen")
         */
        public function AdminCreateTicketAction(Request $request) {

            $ticket = new Ticket;
            $form = $this->createForm(TicketType::class, $ticket, array(
                'method' => 'GET'
            ));

            $form->handleRequest($request);

            if ($form->isSubmitted()) {

                $em = $this->getDoctrine()->getManager();
                $em->persist($ticket);
                $em->flush();

                return $this->redirectToRoute('measure_servers');
            }

            return $this->render( 'sysadmin/pages/AdminCreateTicket.html.twig', array(
                    'ticket' => $form->createView(),
                )
            );
        }
</pre>

【问题讨论】:

  • 在命令app/console doctrine:schema:update --force 之后,您是否将表inventory_computer_join 放入数据库中?如果您转储变量 $ticket 在保留您的票之前,您是否有计算机链接到它?你的问题到底出在哪里:-)

标签: php symfony doctrine-orm orm many-to-many


【解决方案1】:

如果你想存储ManyToMany试试吧;

第一:

在 Ticket.php 实体中将 protected $commputer; 更改为 protected $computers。 因为,您在 addComputer() 或 removeComputer() 使用 $this->computers 。

第二个:* @ORM\ManyToMany(targetEntity="Ticket", mappedBy="computer")mappedBy="computers" 在 Computer.php 实体。

你可以用电脑存票,在Controller上试试: $ticket = new Ticket(); $computer = new Computer();

$ticket->setCreator(4);
$ticket->setOwner(3);
$computer->addTicket($ticket);
$ticket->addComputer($computer);

$em = $this->getDoctrine()->getManager();
$em->persist($computer);
$em->persist($ticket);
$em->flush();

【讨论】:

    猜你喜欢
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    相关资源
    最近更新 更多