gpt4 book ai didi

php - Doctrine 一对多自引用不起作用

转载 作者:行者123 更新时间:2023-12-02 00:42:58 24 4
gpt4 key购买 nike

求助。我正在尝试在我的应用程序中执行一对多实体,我阅读了一份 Doctrine 文档。我有我的 MenuItem 类,它与页表有一对一的关系,与表菜单有多对多的关系。我必须得到一个树形菜单,所以 MenuItem 类有 ID(对我来说是 child )和 parent ,但在更新模式后它不起作用,我在数据库中的父字段仍然不是外键。这是我的 MenuItem.php 类代码:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* MenuItem
*
* @ORM\Table(name="menu_item")
* @ORM\Entity(repositoryClass="AppBundle\Repository\MenuItemRepository")
*/
class MenuItem
{
/**
* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\OneToMany(targetEntity="MenuItem", mappedBy="parentId")
*/
private $id;

/**
* @var string
* @ORM\Column(name="url", type="string", length=255, unique=true, nullable=true)
*/
private $url;

/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;

/**
* @var int
* @ORM\Column(name="parent", type="integer", nullable=true)
* @ORM\ManyToOne(targetEntity="MenuItem", inversedBy="id")
* @ORM\JoinColumn(name="parent", referencedColumnName="id")
*/
private $parentId;

/**
* Set url
*
* @param string $url
*
* @return MenuItem
*/
public function setUrl($url)
{
$this->url = $url;

return $this;
}

/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}

/**
* Set name
*
* @param string $name
*
* @return MenuItem
*/
public function setName($name)
{
$this->name = $name;

return $this;
}

/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* @ORM\ManyToMany(targetEntity="Menu", mappedBy="menuitems")
*/
private $menus;
/**
* Constructor
*/
public function __construct()
{
$this->menus = new \Doctrine\Common\Collections\ArrayCollection();
$this->id = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
* Add menu
*
* @param \AppBundle\Entity\Menu $menu
*
* @return MenuItem
*/
public function addMenu(\AppBundle\Entity\Menu $menu)
{
$this->menus[] = $menu;

return $this;
}

/**
* Remove menu
*
* @param \AppBundle\Entity\Menu $menu
*/
public function removeMenu(\AppBundle\Entity\Menu $menu)
{
$this->menus->removeElement($menu);
}

/**
* Get menus
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getMenus()
{
return $this->menus;
}

/**
* @ORM\OneToOne(targetEntity="Page")
*/
private $page;

/**
* Set page
*
* @param \AppBundle\Entity\Page $page
*
* @return MenuItem
*/
public function setPage(\AppBundle\Entity\Page $page = null)
{
$this->page = $page;

return $this;
}

/**
* Get page
*
* @return \AppBundle\Entity\Page
*/
public function getPage()
{
return $this->page;
}

/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}

/**
* Set parent
*
* @param integer $parent
*
* @return MenuItem
*/
public function setParentId($parentId)
{
$this->parentId = $parentId;

return $this;
}

/**
* Get parent
*
* @return integer
*/
public function getParentId()
{
return $this->parentId;
}
}

最佳答案

尝试使用这个:

    /**
* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

//other fields

/**
* One MenuItem has Many MenuItems.
* @ORM\OneToMany(targetEntity="MenuItem", mappedBy="parent")
*/
private $children;

/**
* Many MenuItems have One MenuItem.
* @ORM\ManyToOne(targetEntity="MenuItem", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true)
*/
private $parent;

我删除了 $id 字段中的关系并添加了 children 字段

关于php - Doctrine 一对多自引用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45563694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com