PHP主义加载关联对象导致错误

时间:2019-04-04 20:28:24

标签: php doctrine-orm

我有两个对象,分别名为OsobaAdresa。他们两个都正确地映射了学说注释。 OsobaAdresa一对一关联。

我可以通过实体管理器分别加载Osoba和Adresa。只要Osoba没有填充Adresa的属性,它就可以正常工作。但是,当我使用属性Adresa保存Osoba并尝试从数据库中检索对象时,就会引发错误。

这就是我尝试获取对象Osoba的方式。数据库中ID为13的Osoba包含一个指向Adresa表的ID。

$osoba = $entityManager->find("Osoba", 13);

抛出的错误是

Fatal error: require(): Failed opening required 'C:\Users\xxx\AppData\Local\Temp\__CG__Adresa.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\vendor\doctrine\common\lib\Doctrine\Common\Proxy\AbstractProxyFactory.php on line 206

这是我的Osoba和Adresa实体。

<?php
declare(strict_types = 1);

require_once('Krajina.php');
require_once('Obec.php');
require_once ("BaseDAO.php");

use \Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity
 * @ORM\Table(name="mr_adresa")
 */
class Adresa extends BaseDAO
{

/**
 * @ORM\Column(type="string", name="orientacne_cislo")
*/
private $orientacneCislo;
/**
 * @ORM\Column(type="string")
 */
private $psc;
/**
 * @ORM\Column(type="string", name="supisne_cislo")
 */
private $supisneCislo;
/**
 * @ORM\Column(type="string")
 */
private $ulica;
/**
 * @ORM\OneToOne(targetEntity="Krajina")
 * @ORM\JoinColumn(name="krajina_id", referencedColumnName="id")
 */
private $m_Krajina;
/**
 * @ORM\OneToOne(targetEntity="Obec")
 * @ORM\JoinColumn(name="obec_id", referencedColumnName="id")
 */
private $m_Obec;

public function getOrientacneCislo(): string
{
    return $this->orientacneCislo;
}

public function setOrientacneCislo(string $orientacneCislo): void
{
    $this->orientacneCislo = $orientacneCislo;
}

public function getPsc(): string
{
    return $this->psc;
}

public function setPsc(string $psc): void
{
    $this->psc = $psc;
}

public function getSupisneCislo(): string
{
    return $this->supisneCislo;
}

public function setSupisneCislo(string $supisneCislo): void
{
    $this->supisneCislo = $supisneCislo;
}

public function getUlica(): string
{
    return $this->ulica;
}

public function setUlica(string $ulica): void
{
    $this->ulica = $ulica;
}

public function getMKrajina(): Krajina
{
    return $this->m_Krajina;
}

public function setMKrajina(Krajina $m_Krajina): void
{
    $this->m_Krajina = $m_Krajina;
}

public function getMObec(): Obec
{
    return $this->m_Obec;
}

public function setMObec(Obec $m_Obec): void
{
    $this->m_Obec = $m_Obec;
}

}

<?php
declare(strict_types = 1);

require_once('Krajina.php');
require_once('Pohlavie.php');
require_once('Adresa.php');
require_once ("BaseDAO.php");

use \Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="mr_osoba")
*/
class Osoba extends BaseDAO
{

    /**
     * @ORM\Column(type="date", name="datum_narodenia")
     */
    private $datumNarodenia;
    /**
     * @ORM\Column(type="string")
     */
    private $email;
    /**
     * @ORM\Column(type="string", name="iban_dotacia")
     */
    private $ibanDotacia;
    /**
     * @ORM\Column(type="string", name="iban_sponzor")
     */
    private $ibanSponzor;
    /**
     * @ORM\Column(type="string")
    */
    private $meno;
    /**
     * --@ORM\Column(type="datetime", name="platnost_ku_datumu")
     */
//  private $platnostKuDatumu;
    /**
     * @ORM\Column
     */
    private $priezvisko;
    /**
     * @ORM\Column(name="rodne_cislo")
     */
    private $rodneCislo;
    /**
     * @ORM\OneToOne(targetEntity="Krajina")
     * @ORM\JoinColumn(name="krajina_id", referencedColumnName="id")
     */
    private $m_Krajina;
    /**
     * @ORM\Column(name="pohlavie", type="string")
     */
    private $m_Pohlavie;
    /**
     * @ORM\OneToOne(targetEntity="Krajina")
     * @ORM\JoinColumn(name="statna_prislusnost_id", referencedColumnName="id")
     */
    private $m_StatnaPrislusnost;
    /**
     * @ORM\OneToOne(targetEntity="Adresa")
     * @ORM\JoinColumn(name="dorucovacia_adresa_id", referencedColumnName="id")
     */
    private $m_DorucovaciaAdresa;
    /**
     * @ORM\OneToOne(targetEntity="Adresa")
     * @ORM\JoinColumn(name="trvale_bydlisko_id", referencedColumnName="id")
     */
    private $m_TrvaleBydlisko;

    public function getDatumNarodenia()
    {
        return $this->datumNarodenia;
    }

    public function setDatumNarodenia($datumNarodenia): void
    {
        $this->datumNarodenia = $datumNarodenia;
    }

    public function getEmail(): string
    {
        return $this->email;
    }

    public function setEmail(string $email): void
    {
        $this->email = $email;
    }

    public function getIbanDotacia(): string
    {
        return $this->ibanDotacia;
    }

    public function setIbanDotacia(string $ibanDotacia): void
    {
        $this->ibanDotacia = $ibanDotacia;
    }

    public function getIbanSponzor(): string
    {
        return $this->ibanSponzor;
    }

    public function setIbanSponzor(string $ibanSponzor): void
    {
        $this->ibanSponzor = $ibanSponzor;
    }

    public function getMeno(): string
    {
        return $this->meno;
    }

    public function setMeno(string $meno)
    {
        $this->meno = $meno;
    }

    public function getPlatnostKuDatumu(): DateTime
    {
        return $this->platnostKuDatumu;
    }

    public function setPlatnostKuDatumu(DateTime $platnostKuDatumu): void
    {
        $this->platnostKuDatumu = $platnostKuDatumu;
    }

    public function getPriezvisko(): string
    {
        return $this->priezvisko;
    }

    public function setPriezvisko(string $priezvisko): void
    {
        $this->priezvisko = $priezvisko;
    }

    public function getRodneCislo(): string
    {
        return $this->rodneCislo;
    }

    public function setRodneCislo(string $rodneCislo): void
    {
        $this->rodneCislo = $rodneCislo;
    }

    public function getMKrajina(): Krajina
    {
        return $this->m_Krajina;
    }

    public function setMKrajina(Krajina $m_Krajina): void
    {
        $this->m_Krajina = $m_Krajina;
    }

    public function getMPohlavie(): Pohlavie
    {
        return $this->m_Pohlavie;
    }

    public function setMPohlavie(Pohlavie $m_Pohlavie): void
    {
        $this->m_Pohlavie = $m_Pohlavie;
    }

    public function getMStatnaPrislusnost(): Krajina
    {
        return $this->m_StatnaPrislusnost;
    }

    public function setMStatnaPrislusnost(Krajina $m_StatnaPrislusnost): void
    {
        $this->m_StatnaPrislusnost = $m_StatnaPrislusnost;
    }

    public function getMDorucovaciaAdresa(): Adresa
    {
        return $this->m_DorucovaciaAdresa;
    }

    public function setMDorucovaciaAdresa(Adresa $m_Adresa): void
    {
        $this->m_DorucovaciaAdresa = $m_Adresa;
    }

    public function getMTrvaleBydlisko(): Adresa
    {
        return $this->m_TrvaleBydlisko;
    }

    public function setMTrvaleBydlisko(Adresa $m_TrvaleBydlisko): void
    {
        $this->m_TrvaleBydlisko = $m_TrvaleBydlisko;
    }

}

1 个答案:

答案 0 :(得分:0)

好的,我发现了问题所在。

Doctrine通过由代理本身生成的代理对象连接到数据库。在为配置文件创建配置时,有两个选项-将dev模式设置为true或false。当开发人员模式设置为false时,它不会每次都生成代理,而是仅生成一次代理。因此,您需要至少将dev模式设置为true一次,或者自己生成类,对此答案进行了解释:https://stackoverflow.com/a/20231349/1869111