Symfony2 Doctrine 重复主键

Symfony2 Doctrine duplicate primary key

对于我的一个实体,我突然无法将记录添加到特定 table。插入语句没问题,问题出在主键上,这应该完全由学说来处理,我没有代码来干涉它。所以我不知道这里到底发生了什么......

这是其他 PHP/db 版本等的已知效果吗?

        ... code for setting values of $extraOpening's properties ...

        $em = $this->getDoctrine()->getManager();
        $em->persist($extraOpening);

        $em->flush($extraOpening);

[2017-09-18 11:05:47] request.CRITICAL: Uncaught PHP Exception Doctrine\DBAL\Exception\UniqueConstraintViolationException: "An exception occurred while executing 'INSERT INTO extra_opening (open, close, arrOrDep, callsign, reason, additional, separateOpening, comment, price, debit_period) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["2017-09-08 10:50:00", "2017-09-08 10:50:00", "Ospec.", null, "mw test", null, 1, null, 1034, 117]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY'"

--- 更新 2 ---

添加我的实体(我刚刚发布的小更新是错误的,所以请无视你是否有时间阅读它......)。

我使用 phpmyadmin 导出和相同的导入工具移动了数据库。也许我在关系或 pKeys 方面设置错误,但我已经做了一百次并且没有做任何超出默认设置的事情...

namespace ExtraOpeningBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * ExtraOpening
 *
 * @ORM\Table(name="extra_opening")
 * @ORM\Entity(repositoryClass="ExtraOpeningBundle\Repository\ExtraOpeningRepository")
 */
class ExtraOpening
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="open", type="datetime")
     */
    private $open;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="close", type="datetime")
     */
    private $close;

    /**
     * @var string
     *
     * @ORM\Column(name="arrOrDep", type="string", length=6, nullable=true)
     */
    private $arrOrDep;

    /**
     * @var string
     *
     * @ORM\Column(name="callsign", type="string", length=30, nullable=true)
     */
    private $callsign;

    /**
     * @var string
     *
     * @ORM\Column(name="reason", type="string", length=255, nullable=true)
     */
    private $reason;

    /**
     * @var int
     *
     * @ORM\Column(name="additional", type="integer", nullable=true)
     */
    private $additional;

    /**
     * @var bool
     *
     * @ORM\Column(name="separateOpening", type="boolean")
     */
    private $separateOpening;

    /**
     * @var string
     *
     * @ORM\Column(name="comment", type="text", nullable=true)
     */
    private $comment;

    /**
     * @var int
     *
     * @ORM\Column(name="price", type="integer", nullable=true)
     */
    private $price;

    /**
    * @var object \ExtraOpeningBundle\Entity\DebitPeriod
    *  
    * @ORM\ManyToOne(targetEntity="\ExtraOpeningBundle\Entity\DebitPeriod", inversedBy="extraOpenings")
    * @ORM\JoinColumn(name="debit_period", referencedColumnName="id", nullable=false)
    */
    protected $debitPeriod;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set open
     *
     * @param \DateTime $open
     *
     * @return ExtraOpening
     */
    public function setOpen($open)
    {
        $this->open = $open;

        return $this;
    }

    /**
     * Get open
     *
     * @return \DateTime
     */
    public function getOpen()
    {
        return $this->open;
    }

    /**
     * Set close
     *
     * @param \DateTime $close
     *
     * @return ExtraOpening
     */
    public function setClose($close)
    {
        $this->close = $close;

        return $this;
    }

    /**
     * Get close
     *
     * @return \DateTime
     */
    public function getClose()
    {
        return $this->close;
    }

    /**
     * Set arrOrDep
     *
     * @param string $arrOrDep
     *
     * @return ExtraOpening
     */
    public function setArrOrDep($arrOrDep)
    {
        $this->arrOrDep = $arrOrDep;

        return $this;
    }

    /**
     * Get arrOrDep
     *
     * @return string
     */
    public function getArrOrDep()
    {
        return $this->arrOrDep;
    }

    /**
     * Set callsign
     *
     * @param string $callsign
     *
     * @return ExtraOpening
     */
    public function setCallsign($callsign)
    {
        $this->callsign = $callsign;

        return $this;
    }

    /**
     * Get callsign
     *
     * @return string
     */
    public function getCallsign()
    {
        return $this->callsign;
    }

    /**
     * Set reason
     *
     * @param string $reason
     *
     * @return ExtraOpening
     */
    public function setReason($reason)
    {
        $this->reason = $reason;

        return $this;
    }

    /**
     * Get reason
     *
     * @return string
     */
    public function getReason()
    {
        return $this->reason;
    }

    /**
     * Set additional
     *
     * @param integer $additional
     *
     * @return ExtraOpening
     */
    public function setAdditional($additional)
    {
        $this->additional = $additional;

        return $this;
    }

    /**
     * Get additional
     *
     * @return int
     */
    public function getAdditional()
    {
        return $this->additional;
    }

    /**
     * Set separateOpening
     *
     * @param boolean $separateOpening
     *
     * @return ExtraOpening
     */
    public function setSeparateOpening($separateOpening)
    {
        $this->separateOpening = $separateOpening;

        return $this;
    }

    /**
     * Get separateOpening
     *
     * @return bool
     */
    public function getSeparateOpening()
    {
        return $this->separateOpening;
    }

    /**
     * Set price
     *
     * @param integer $price
     *
     * @return ExtraOpening
     */
    public function setPrice($price)
    {
        $this->price = $price;

        return $this;
    }

    /**
     * Get price
     *
     * @return int
     */
    public function getPrice()
    {
        return $this->price;
    }


    /**
     * Get debitTime
     *
     * @return string
     */
    public function getDebitTime()
    {
        $open = $this->getOpen();
        $close = $this->getClose();

        $interval = date_diff($open, $close);

        $r = $interval->format('%H:%I');

        return $r;
    }


    /**
     * Get debitTimeSeconds
     *
     * @return int
     */
    public function getDebitTimeSeconds()
    {
        $open = $this->getOpen();
        $close = $this->getClose();

        $r = $close->format('U') - $open->format('U');

        return $r;
    }

    /**
     * Get debitHours
     *
     * @return decimal
     */
    public function getDebitHours()
    {
        $open = $this->getOpen();
        $close = $this->getClose();

        $seconds = date_timestamp_get($close) - date_timestamp_get($open);

        $hours = $seconds / 3600;

        if($this->getSeparateOpening() && $hours < 3) {
            $hours = 3;
        }

        return number_format($hours,2);
    }


    /**
     * Get debitAmount
     *
     * @return decimal
     */
    public function getDebitAmount()
    {
        $hours = $this->getDebitHours();
        $price = $this->getPrice();

        $amount = $hours * $price;

        //return number_format($amount,2);   
        return $amount;
    }

    /**
     * Set comment
     *
     * @param string $comment
     *
     * @return ExtraOpening
     */
    public function setComment($comment)
    {
        $this->comment = $comment;

        return $this;
    }

    /**
     * Get comment
     *
     * @return string
     */
    public function getComment()
    {
        return $this->comment;
    }


    /**
     * Get acrGroup
     *
     * @return \HazardlogBundle\Entity\ACRGroup
     */
    public function getAcrGroup()
    {
        return $this->getDebitPeriod()->getACRGroup();
    }


    /**
     * Set debitPeriod
     *
     * @param \ExtraOpeningBundle\Entity\DebitPeriod $debitPeriod
     *
     * @return ExtraOpening
     */
    public function setDebitPeriod(\ExtraOpeningBundle\Entity\DebitPeriod $debitPeriod)
    {
        $this->debitPeriod = $debitPeriod;

        return $this;
    }

    /**
     * Get debitPeriod
     *
     * @return \ExtraOpeningBundle\Entity\DebitPeriod
     */
    public function getDebitPeriod()
    {
        return $this->debitPeriod;
    }
}

正如 m-khalid-junaid https://whosebug.com/users/853360/m-khalid-junaid 指出的那样,导入已损坏并且 auto_increment 属性 没有被注意到,它没有被设置。谢谢。