无法解析类的列类型。

huangapple go评论53阅读模式
英文:

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

    &lt;?php 
require_once &#39;vendor/autoload.php&#39;;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\ORMSetup;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\Collections\ArrayCollection;
include &#39;src/Uczen.php&#39;;
include &#39;src/Klasa.php&#39;;
$config = ORMSetup::createAnnotationMetadataConfiguration(array(__DIR__  . &#39;/src&#39;));
$conn = array(
&#39;dbname&#39; =&gt; &#39;zi2&#39;,
&#39;user&#39; =&gt; &#39;root&#39;,
&#39;password&#39; =&gt; &#39;user&#39;,
&#39;host&#39; =&gt; &#39;localhost&#39;,
&#39;driver&#39; =&gt; &#39;pdo_mysql&#39;,
&#39;charset&#39; =&gt; &#39;utf8&#39;
);
$em = EntityManager::create($conn, $config);
$k1 = new Klasa();
$k1-&gt;setId(1);
$k1-&gt;setNazwa(&quot;312B&quot;);
$k1-&gt;setRok(3);
$u1 = new Uczen();
$u1-&gt;setId(1);
$u1-&gt;setImie(&#39;Jan&#39;);
$u1-&gt;setNazwisko(&#39;Kowalski&#39;);
// tablice
$uczniowie1 = new ArrayCollection([$u1]); // uczniowie klasy k1
// Foreign keys
$k1-&gt;setUczniowie($uczniowie1);
$u1-&gt;setKlasa($k1);
$em-&gt;persist($k1);
$em-&gt;persist($u1);
$em-&gt;flush();
?&gt;

Uczen.php

&lt;?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=&quot;integer&quot;) 
* @GeneratedValue
*/
protected $id_uczen;
/** 
* @Column(type=&quot;string&quot;) 
*/
protected $imie;
/** 
* @Column(type=&quot;string&quot;) 
*/
protected $nazwisko;
/** 
* @ManyToOne(targetEntity=&quot;Klasa&quot;, inversedBy=&quot;uczniowie&quot;)
*/
protected $klasa;
public function setId($id){
$this-&gt;id_uczen = $id;
}
public function getId(){
return $this-&gt;id_uczen;
}
public function setImie($imie){
$this-&gt;imie = $imie;
}
public function getImie(){
return $this-&gt;imie;
}
public function setNazwisko($nazwisko){
$this-&gt;nazwisko = $nazwisko;
}
public function getNazwisko(){
return $this-&gt;nazwisko;
}
public function setKlasa($klasa){
$this-&gt;klasa = $klasa;
}
public function getKlasa(){
return $this-&gt;klasa;
}
}
?&gt;

Klasa.php

&lt;?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=&quot;integer&quot;) 
* @GeneratedValue
*/
protected $id_klasa;
/** 
* @Column(type=&quot;string&quot;) 
*/
protected $nazwa;
/** 
* @Column(type=&quot;integer&quot;) 
*/
protected $rok;
/** 
* @OneToMany(targetEntity=&quot;Uczen&quot;,mappedBy=&quot;klasa&quot;)
*/
protected $uczniowie;
public function __construct(){
$this-&gt;uczniowie = new ArrayCollection();
}
public function setId($id){
$this-&gt;id_klasa = $id;
}
public function getId(){
return $this-&gt;id_klasa;
}
public function setNazwa($nazwa){
$this-&gt;nazwa = $nazwa;
}
public function getNazwa(){
return $this-&gt;nazwa;
}
public function setRok($rok){
$this-&gt;rok = $rok;
}
public function getRok(){
return $this-&gt;rok;
}
public function setUczniowie($uczniowie){
$this-&gt;uczniowie = $uczniowie;
}
public function getUczniowie(){
return $this-&gt;uczniowie;
}
}
?&gt;

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=&quot;Klasa&quot;,inversedBy=&quot;uczniowie&quot;,cascade={&quot;persist&quot;})
* @JoinColumn(name=&quot;klasa&quot;,nullable=false,referencedColumnName=&quot;id_klasa&quot;)
*/
protected $klasa;

huangapple
  • 本文由 发表于 2023年6月21日 23:56:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定