【发布时间】:2017-09-18 09:25:00
【问题描述】:
对于我的一个实体,我突然无法将记录添加到特定表中。插入语句没问题,问题出在主键上,应该完全按原则处理,我没有代码可以干预。所以我不知道这里到底发生了什么......
这是其他东西(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(打开、关闭、arrOrDep、呼号、原因)时发生异常, 附加的, 单独的Opening, 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]:违反完整性约束:1062 重复条目 '0 ' 代表键 '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;
}
}
【问题讨论】:
-
你能显示你的实体定义吗?看起来 id 生成没有按原样完成
-
添加了实体。你可以猜到,第一个值 (id) 是 pKey,它被教义奇怪地设置为 0...
-
extra_opening 中的 id col 是否设置为自动递增?
-
确实不是!但是在导入时肯定有问题,因为旧数据库确实有它。可以肯定的是,我将不得不重新导入整个内容,以防出现更多未正确导入的结构性问题。谢谢!
-
你的数据库是什么?
标签: symfony doctrine duplicates primary-key