问题描述
我正在尝试使用PHP bin/console doctrine:schema:update --force
更新数据库架构
但我仍然遇到相同的错误:
引用了列名称
id
以从App \ Entity \ Students建立关系 对App \ Entity \ Classes不存在。
我该如何解决?
学生班:
<?PHP
namespace App\Entity;
use App\Repository\StudentsRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=StudentsRepository::class)
*/
class Students
{
/**
* @ORM\id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $studentID;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Classes",inversedBy="students")
*/
private $classe;
public function getclasse(): ?Classes
{
return $this->classe;
}
public function setclasse(?Classes $classe): self
{
$this->classes = $classe;
return $this;
}
public function getstudentID(): ?int
{
return $this->studentID;
}
}
Classes类:
<?PHP
namespace App\Entity;
use App\Repository\ClassesRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=ClassesRepository::class)
*/
class Classes
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $classID;
/**
* @ORM\OnetoMany(targetEntity="App\Entity\Students",mappedBy="classe")
*/
private $students;
public function __construct()
{
$this->students = new ArrayCollection();
}
/**
* @return Collection/Students[]
*/
public function getstudents(): Collection
{
return $this->students;
}
解决方法
我不熟悉该学说,但看来您正在尝试建立双向关系。 According to the docs,for a bidirectional one-to-many relationship:
这种双向映射需要在一侧具有mapledBy属性,而在多个方面则需要inversedBy属性。
class Students {
/**
*
* @ManyToOne(targetEntitity="App\Entity\Classes",mappedBy="students")
* @JoinColumn(name="class_id",referencedColumnName="class_id")
*/
private $classes;
}
class Classes {
/**
* @OneToMany(targetEntitity="App\Entity\Students",mappedBy="classes")
*/
private $students;
}
请注意学生身上的@joinColumn
。我相信您正在通过在学生班级上指定$studentID
在班级上指定$classID
来更改默认主键。因此,您必须指定要加入的列。
自以为是,所以请带一点盐...
FWIW,我建议使用默认的主键,这通常是人们所做的事情(我会说这是行业标准/预期,但我确信人们不同意)。
此外,根据您实体的名称,我希望它是多对多关系;但是,我不知道您的具体规格。