Symfony2 Doctrine重复主键

时间:2017-09-18 09:25:00

标签: symfony doctrine duplicates primary-key

对于我的一个实体,我突然不能将记录添加到特定的表中。 insert语句没问题,问题出在主键上,而且应该完全由doctrine处理,我没有代码可以干掉它。所以我对这里发生的事情感到茫然......

这是其他东西,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:未捕获PHP异常Doctrine \ DBAL \ Exception \ UniqueConstraintViolationException:“执行'INSERT INTO extra_opening时发生异常(open,close,arrOrDep,callsign,reason ,附加,单独开启,评论,价格,debit_period)VALUES(?,?,?,?,?,?,?,?,?,?)'与params [“2017-09-08 10:50:00”, “2017-09-08 10:50:00”,“Ospec。”,null,“mw test”,null,1,null,1034,117]:SQLSTATE [23000]:完整性约束违规:1062重复条目'0 '为关键'PRIMARY'“

---更新2 ---

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

我使用phpmyadmin export和相同的导入工具移动数据库。也许我对关系或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;
    }
}

1 个答案:

答案 0 :(得分:0)

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