duanrenchuo9244
2019-04-04 20:28
浏览 69
已采纳

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 18: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

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题