Doctrine:如何将值设置为null而不是实体

问题描述 投票:0回答:1

我的学说实体有问题。

在我的模块中,我有一个历史记录可以列出用户执行的操作。

在此过程中,我使用实体与自己之间的ManyToOne关系。

当我更新数据库中的项目时,我使用“ setGain()”将该结果引用到另一个结果,但是如果我想取消用户的操作,则需要将setGain()的值设置为null,但是将不接受并返回:

PHP Catchable fatal error: Argument 1 passed to Paris\Entity\Historique::setGain() must be an instance of Paris\Entity\Historique, null given

我的实体是:

<?php

namespace Paris\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Historique
 *
 * @ORM\Table(name="paris__historique")
 * @ORM\Entity
 */
class Historique {
    /**
     * @var integer
     *
     * @ORM\Column(name="id_historique", type="integer", precision=0, scale=0, nullable=false, unique=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id_historique;

    /**
     * @var integer
     *
     * @ORM\Column(name="id_jol", type="integer", length=11, precision=0, scale=0, nullable=true, unique=false)
     */
    private $id_jol;

    /**
     * @var string
     *
     * @ORM\Column(name="action", type="string", length=50, precision=0, scale=0, nullable=false, unique=false)
     */
    private $action;

    /**
     * @var string
     *
     * @ORM\Column(name="detail", type="text", precision=0, scale=0, nullable=true, unique=false)
     */
    private $detail;

    /**
     * @var integer
     *
     * @ORM\ManyToOne(targetEntity="\Paris\Entity\Historique")
      * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id_gain", referencedColumnName="id_historique", nullable=true)
     * })
     */
    private $id_gain;

    /**
     * @var integer
     *
     * @ORM\ManyToOne(targetEntity="\Paris\Entity\Parieur")
      * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id_participant", referencedColumnName="id_participant", nullable=true)
     * })
     */
    private $id_participant;

    /**
     * @var integer
     *
     * @ORM\ManyToOne(targetEntity="\Paris\Entity\Concours")
      * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id_concours", referencedColumnName="id_concours", nullable=true)
     * })
     */
    private $id_concours;

    /**
     * @var integer
     *
     * @ORM\ManyToOne(targetEntity="\Paris\Entity\Periode")
      * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id_periode", referencedColumnName="id_periode", nullable=true)
     * })
     */
    private $id_periode;

    /**
     * @var integer
     *
     * @ORM\ManyToOne(targetEntity="\Paris\Entity\Paris")
      * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id_paris", referencedColumnName="id_paris", nullable=true)
     * })
     */
    private $id_paris;

     /**
     * @var integer
     *
     * @ORM\ManyToOne(targetEntity="\Paris\Entity\Mise")
      * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id_mise", referencedColumnName="id_mise", nullable=true)
     * })
     */
    private $id_mise;

    /**
     * @var integer
     *
     * @ORM\Column(name="date_historique", type="integer", length=11, precision=0, scale=0, nullable=false, unique=false)
     */
    private $date_historique;


    public function getIdHistorique() {
        return $this->id_historique;
    }

    public function setIdJol($idJol) {
        $this->id_jol = $idJol;

        return $this;
    }

    public function getIdJol() {
        return $this->id_jol;
    }

    public function setAction($action) {
        $this->action = $action;

        return $this;
    }

    public function getAction() {
        return $this->action;
    }

    public function getGain() {
        return $this->id_gain;
    }

    public function setGain(\Paris\Entity\Historique $id_gain) {
        $this->id_gain = $id_gain;
        return $this;
    }

    public function setDetail($detail) {
        $this->detail = $detail;

        return $this;
    }

    public function getDetail() {
        return $this->detail;
    }

    public function setParieur(\Paris\Entity\Parieur $id_participant) {
        $this->id_participant = $id_participant;
        return $this;
    }

    public function getParieur() {
        return $this->id_participant;
    }

    public function setConcours(\Paris\Entity\Concours $id_concours) {
        $this->id_concours = $id_concours;
        return $this;
    }

    public function getConcours() {
        return $this->id_concours;
    }

    public function setPeriode(\Paris\Entity\Periode $id_periode) {
        $this->id_periode = $id_periode;
        return $this;
    }

    public function getPeriode() {
        return $this->id_periode;
    }

    public function setPari(\Paris\Entity\Paris $id_paris) {
        $this->id_paris = $id_paris;
        return $this;
    }

    public function getPari() {
        return $this->id_paris;
    }

    public function setmise(\Paris\Entity\Mise $id_mise) {
        $this->id_mise = $id_mise;
        return $this;
    }

    public function getMise() {
        return $this->id_mise;
    }

    public function setDate($dateHistorique) {
        $this->date_historique = $dateHistorique;

        return $this;
    }

    public function getDate() {
        return $this->date_historique;
    }
}

在我的数据库中,关联的字段默认情况下正确设置为Null,并且可为Nullable。

您能帮我吗? ^^

谢谢您的回答。

php doctrine zend-framework3
1个回答
0
投票

setGain定义为:

public function setGain(?\Paris\Entity\Historique $id_gain) {
    $this->id_gain = $id_gain;
    return $this;
}

typehint类前面的[?允许将null作为参数传递。