首页 > 解决方案 > 原则防止数据库中不存在记录的 OneToOne 关联错误

问题描述

问题

我正在尝试使用 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()

我试过的/我看过的答案


补充说明:

问题:

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

标签: doctrine-ormlaravel-doctrine

解决方案


推荐阅读