英文:
Could not resolve type of column of class
问题
我想建立一个一对多的关系。一个 'Uczen' 应该有一个 'klasa',而一个 'Klasa' 应该有多个 'Uczen'。我正在使用Doctrine。
index.php
<?php
require_once 'vendor/autoload.php';
use Doctrine\ORM\ORMException;
use Doctrine\ORM\ORMSetup;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\Collections\ArrayCollection;
include 'src/Uczen.php';
include 'src/Klasa.php';
$config = ORMSetup::createAnnotationMetadataConfiguration(array(__DIR__ . '/src'));
$conn = array(
'dbname' => 'zi2',
'user' => 'root',
'password' => 'user',
'host' => 'localhost',
'driver' => 'pdo_mysql',
'charset' => 'utf8'
);
$em = EntityManager::create($conn, $config);
$k1 = new Klasa();
$k1->setId(1);
$k1->setNazwa("312B");
$k1->setRok(3);
$u1 = new Uczen();
$u1->setId(1);
$u1->setImie('Jan');
$u1->setNazwisko('Kowalski');
// 数组
$uczniowie1 = new ArrayCollection([$u1]); // k1班级的学生
// 外键
$k1->setUczniowie($uczniowie1);
$u1->setKlasa($k1);
$em->persist($k1);
$em->persist($u1);
$em->flush();
?>
Uczen.php
<?php
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\ManyToOne;
/**
* @Entity
*/
class Uczen
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue
*/
protected $id_uczen;
/**
* @Column(type="string")
*/
protected $imie;
/**
* @Column(type="string")
*/
protected $nazwisko;
/**
* @ManyToOne(targetEntity="Klasa", inversedBy="uczniowie")
*/
protected $klasa;
public function setId($id)
{
$this->id_uczen = $id;
}
public function getId()
{
return $this->id_uczen;
}
public function setImie($imie)
{
$this->imie = $imie;
}
public function getImie()
{
return $this->imie;
}
public function setNazwisko($nazwisko)
{
$this->nazwisko = $nazwisko;
}
public function getNazwisko()
{
return $this->nazwisko;
}
public function setKlasa($klasa)
{
$this->klasa = $klasa;
}
public function getKlasa()
{
return $this->klasa;
}
}
?>
Klasa.php
<?php
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity
*/
class Klasa
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue
*/
protected $id_klasa;
/**
* @Column(type="string")
*/
protected $nazwa;
/**
* @Column(type="integer")
*/
protected $rok;
/**
* @OneToMany(targetEntity="Uczen",mappedBy="klasa")
*/
protected $uczniowie;
public function __construct()
{
$this->uczniowie = new ArrayCollection();
}
public function setId($id)
{
$this->id_klasa = $id;
}
public function getId()
{
return $this->id_klasa;
}
public function setNazwa($nazwa)
{
$this->nazwa = $nazwa;
}
public function getNazwa()
{
return $this->nazwa;
}
public function setRok($rok)
{
$this->rok = $rok;
}
public function getRok()
{
return $this->rok;
}
public function setUczniowie($uczniowie)
{
$this->uczniowie = $uczniowie;
}
public function getUczniowie()
{
return $this->uczniowie;
}
}
?>
错误:
PHP致命错误:在C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\Utility\PersisterHelper.php的第110行找不到类“Klasa”的列“id”的类型
堆栈跟踪:
#0 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php(711):Doctrine\ORM\Utility\PersisterHelper::getTypeOfColumn('id', Object(Doctrine\ORM\Mapping\ClassMetadata), Object(Doctrine\ORM\EntityManager))
#1 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php(736):Doctrine\ORM\Persisters\Entity\BasicEntityPersister->prepareUpdateData(Object(Uczen), true)
#2 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php(270):Doctrine\ORM\Persisters\Entity\BasicEntityPersister->prepareInsertData(Object(Uczen))
#3 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(1144):Doctrine\ORM\Persisters\Entity\BasicEntityPersister->executeInserts()
#4 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(430):Doctrine\ORM\UnitOfWork->executeInserts(Object(Doctrine\ORM\Mapping\ClassMetadata))
#5 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php(403):Doctrine\ORM\UnitOfWork->commit(NULL)
#6 C:\Users\sheil\Desktop\Temp\index.php(41):Doctrine\ORM\EntityManager->flush()
#7 {main}
在C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\Utility\PersisterHelper.php的第110行抛出未捕获的RuntimeException:无法解析类“Klasa”的列“id”的类型
英文:
I want to establish a one to many relashionship. One 'Uczen' should have one 'klasa' and one 'Klasa' should have many 'Uczen'. I am using Doctrine
index.php
<?php
require_once 'vendor/autoload.php';
use Doctrine\ORM\ORMException;
use Doctrine\ORM\ORMSetup;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\Collections\ArrayCollection;
include 'src/Uczen.php';
include 'src/Klasa.php';
$config = ORMSetup::createAnnotationMetadataConfiguration(array(__DIR__ . '/src'));
$conn = array(
'dbname' => 'zi2',
'user' => 'root',
'password' => 'user',
'host' => 'localhost',
'driver' => 'pdo_mysql',
'charset' => 'utf8'
);
$em = EntityManager::create($conn, $config);
$k1 = new Klasa();
$k1->setId(1);
$k1->setNazwa("312B");
$k1->setRok(3);
$u1 = new Uczen();
$u1->setId(1);
$u1->setImie('Jan');
$u1->setNazwisko('Kowalski');
// tablice
$uczniowie1 = new ArrayCollection([$u1]); // uczniowie klasy k1
// Foreign keys
$k1->setUczniowie($uczniowie1);
$u1->setKlasa($k1);
$em->persist($k1);
$em->persist($u1);
$em->flush();
?>
Uczen.php
<?php
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\ManyToOne;
/**
* @Entity
*/
class Uczen
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue
*/
protected $id_uczen;
/**
* @Column(type="string")
*/
protected $imie;
/**
* @Column(type="string")
*/
protected $nazwisko;
/**
* @ManyToOne(targetEntity="Klasa", inversedBy="uczniowie")
*/
protected $klasa;
public function setId($id){
$this->id_uczen = $id;
}
public function getId(){
return $this->id_uczen;
}
public function setImie($imie){
$this->imie = $imie;
}
public function getImie(){
return $this->imie;
}
public function setNazwisko($nazwisko){
$this->nazwisko = $nazwisko;
}
public function getNazwisko(){
return $this->nazwisko;
}
public function setKlasa($klasa){
$this->klasa = $klasa;
}
public function getKlasa(){
return $this->klasa;
}
}
?>
Klasa.php
<?php
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity
*/
class Klasa
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue
*/
protected $id_klasa;
/**
* @Column(type="string")
*/
protected $nazwa;
/**
* @Column(type="integer")
*/
protected $rok;
/**
* @OneToMany(targetEntity="Uczen",mappedBy="klasa")
*/
protected $uczniowie;
public function __construct(){
$this->uczniowie = new ArrayCollection();
}
public function setId($id){
$this->id_klasa = $id;
}
public function getId(){
return $this->id_klasa;
}
public function setNazwa($nazwa){
$this->nazwa = $nazwa;
}
public function getNazwa(){
return $this->nazwa;
}
public function setRok($rok){
$this->rok = $rok;
}
public function getRok(){
return $this->rok;
}
public function setUczniowie($uczniowie){
$this->uczniowie = $uczniowie;
}
public function getUczniowie(){
return $this->uczniowie;
}
}
?>
Error:
PHP Fatal error: Uncaught RuntimeException: Could not resolve type of column "id" of class "Klasa" in C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\Utility\PersisterHelper.php:110
Stack trace:
#0 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php(711): Doctrine\ORM\Utility\PersisterHelper::getTypeOfColumn('id', Object(Doctrine\ORM\Mapping\ClassMetadata), Object(Doctrine\ORM\EntityManager))
#1 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php(736): Doctrine\ORM\Persisters\Entity\BasicEntityPersister->prepareUpdateData(Object(Uczen), true)
#2 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php(270): Doctrine\ORM\Persisters\Entity\BasicEntityPersister->prepareInsertData(Object(Uczen))
#3 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(1144): Doctrine\ORM\Persisters\Entity\BasicEntityPersister->executeInserts()
#4 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(430): Doctrine\ORM\UnitOfWork->executeInserts(Object(Doctrine\ORM\Mapping\ClassMetadata))
#5 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php(403): Doctrine\ORM\UnitOfWork->commit(NULL)
#6 C:\Users\sheil\Desktop\Temp\index.php(41): Doctrine\ORM\EntityManager->flush()
#7 {main}
thrown in C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\Utility\PersisterHelper.php on line 110
Fatal error: Uncaught RuntimeException: Could not resolve type of column "id" of class "Klasa" in C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\Utility\PersisterHelper.php:110
Stack trace:
#0 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php(711): Doctrine\ORM\Utility\PersisterHelper::getTypeOfColumn('id', Object(Doctrine\ORM\Mapping\ClassMetadata), Object(Doctrine\ORM\EntityManager))
#1 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php(736): Doctrine\ORM\Persisters\Entity\BasicEntityPersister->prepareUpdateData(Object(Uczen), true)
#2 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php(270): Doctrine\ORM\Persisters\Entity\BasicEntityPersister->prepareInsertData(Object(Uczen))
#3 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(1144): Doctrine\ORM\Persisters\Entity\BasicEntityPersister->executeInserts()
#4 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(430): Doctrine\ORM\UnitOfWork->executeInserts(Object(Doctrine\ORM\Mapping\ClassMetadata))
#5 C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php(403): Doctrine\ORM\UnitOfWork->commit(NULL)
#6 C:\Users\sheil\Desktop\Temp\index.php(41): Doctrine\ORM\EntityManager->flush()
#7 {main}
thrown in C:\Users\sheil\Desktop\Temp\vendor\doctrine\orm\lib\Doctrine\ORM\Utility\PersisterHelper.php on line 110
答案1
得分: 0
/**
- @ManyToOne(targetEntity="Klasa", inversedBy="uczniowie", cascade={"persist"})
- @JoinColumn(name="klasa", nullable=false, referencedColumnName="id_klasa")
*/
protected $klasa;
英文:
/**
* @ManyToOne(targetEntity="Klasa",inversedBy="uczniowie",cascade={"persist"})
* @JoinColumn(name="klasa",nullable=false,referencedColumnName="id_klasa")
*/
protected $klasa;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论