duanrenchuo9244 2019-04-04 12:28
浏览 70
已采纳

PHP Doctrine加载关联对象会导致错误

I have two object, named Osoba and Adresa. Both of them are correctly mapped with doctrine annotations. Osoba and Adresa are in one to one association.

I am able to load Osoba and Adresa separately with entity manager. As long as Osoba does not have property Adresa populated, it works fine. But when I save Osoba with property Adresa, and trying to retrieve the object from database, an error is thrown.

This is how I try to fetch the object Osoba. The Osoba with id 13 in database contains an id pointing to Adresa table.

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

And the error thrown is

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

Here are my Osoba and Adresa entities.

<?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条回答 默认 最新

  • doulu0266 2019-04-05 10:54
    关注

    Okay, I found out what the problem was.

    Doctrine connects to database through proxy object that are generated by doctrine itself. There are two options when creating configuration for doctrine - either you set dev mode to true or false. When dev mode is set to false, it does not generate proxies each time, only once. So you need to set dev mode to true at least once, or generate the classes yourself , that is explaied in this answer: https://stackoverflow.com/a/20231349/1869111

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 潜在扩散模型的Unet特征提取
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部