1

问题

我正在尝试使用 Doctrine 在 Laravel 应用程序中创建 OneToOne 关联。尝试访问关联时出现此错误。

Entity of type 'Status' for IDs clientId(1) was not found

版本:

教义:2.7.5

拉拉维尔:7.30.4


代码:

客户端类

 <?php

namespace App\Client;

use App\Person\Person;
use Doctrine\ORM\Mapping as ORM;

/**
 * Class Client
 * @package App\Client
 *
 * @ORM\Entity(repositoryClass="ClientRepository")
 * @ORM\Table(name="client")
 */
class Client extends Person
{

    /**
     * @var Status
     *
     * @ORM\OneToOne(targetEntity="Status", mappedBy="client")
     * @ORM\JoinColumn(name="id", referencedColumnName="client_id", nullable=true)
     */
    protected $status;


    /**
     * @return Status
     */
    public function getStatus()
    {
        return $this->status;
    }
}

状态等级:

<?php

namespace App\Client\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Class Status
 * @package App\Client\Entity
 *
 * @ORM\Entity(readOnly=true)
 * @ORM\Table(name="status_view")
 */
class Status
{
    /**
     * @var int
     *
     * @ORM\Id()
     * @ORM\Column(name="client_id", type="integer")
     */
    protected $clientId;

    /**
     * @var \App\Client\Client
     *
     * @ORM\OneToOne(targetEntity="App\Client\Client", inversedBy="staus")
     * @ORM\JoinColumn(name="client_id", referencedColumnName="id", nullable=true)
     */
    protected $client;

    /**
     * @var string
     *
     * @ORM\Column(name="status", type="string")
     */
    protected $status;
    /**
     * @return string
     */
    public function getStatus()
    {
        return $this->status;
    }
}

调用代码

$client->getStatus()->getStatus()

我试过的/我看过的答案


补充说明:

  • 这是对象在调用 时的样子$client->getStatus(),在下一次->getStatus()调用出错之前。

    DoctrineProxies\__CG__\App\Client\Entity\Status {#2011
         +__isInitialized__: false
         #clientId: 4
         #client: null
         #status: null
         …2
    }
    
  • 您可以看到它是一个ClientProxy 对象,它创建的不是“真正的”对象,这就是为什么它在不使用时出错(未找到 ID 为“状态”类型的实体 clientId(1))fetch="EAGER",因为急切加载了一个真正的对象。看这里

  • Proxy 对象中的以下代码是导致上述错误的原因。这就是为什么我不能try catch在父级('true'Client类)中执行 a 的原因,因为它在调用父级之前会出错。

     /**
      * {@inheritDoc}
      */
     public function getStatus()
     {
    
         $this->__initializer__ && $this->__initializer__->__invoke($this, 'getStatus', []);
    
         return parent::getStatus();
     }
    

问题:

为什么nullable=true没有按预期工作,我应该/可以做些什么来使它工作?

4

0 回答 0