在MySQL中插入数据时,Symfony2.4,doctor,PHP中的update列出现错误
问题-当我将表单提交的数据保存到MySQL时,我想将其保存为-在MySQL中插入数据时,Symfony2.4,doctor,PHP中的update列出现错误,php,symfony,doctrine,Php,Symfony,Doctrine,问题-当我将表单提交的数据保存到MySQL时,我想将其保存为- SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'updated' cannot be null 但是我已经给更新列指定了默认值0000-00-00:00:00,默认情况下它必须采用默认值 我使用“prepersist”技术在将代码保存到MySQL之前添加代码 下面是我的“prepersist”代码段,上面的错误就是从中产生的- public functi
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'updated' cannot be null
但是我已经给更新列指定了默认值0000-00-00:00:00,默认情况下它必须采用默认值
我使用“prepersist”技术在将代码保存到MySQL之前添加代码
下面是我的“prepersist”代码段,上面的错误就是从中产生的-
public function setBeforeInsertData() {
$this->added = new \DateTime();
}
我使用了下面的代码来克服这个错误,只是暂时我知道这是一个错误的方法
public function setBeforeInsertData() {
$this->added = new \DateTime();
$this->updated = new \DateTime();
}
以下是各表的实体-
<?php
namespace Skerp\InventoryBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* InventoryLocation
*
* @ORM\Table(name="inventory_location")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
*/
class InventoryLocation
{
/**
* @var string
*
* @ORM\Column(name="locName", type="string", length=50, nullable=false)
*/
private $locname;
/**
* @var string
*
* @ORM\Column(name="address1", type="string", length=50, nullable=false)
*/
private $address1;
/**
* @var string
*
* @ORM\Column(name="address2", type="string", length=50, nullable=true)
*/
private $address2;
/**
* @var string
*
* @ORM\Column(name="address3", type="string", length=50, nullable=true)
*/
private $address3;
/**
* @var string
*
* @ORM\Column(name="city", type="string", length=40, nullable=false)
*/
private $city;
/**
* @var string
*
* @ORM\Column(name="zipCode", type="string", length=5, nullable=false)
*/
private $zipcode;
/**
* @var string
*
* @ORM\Column(name="state", type="string", length=40, nullable=false)
*/
private $state;
/**
* @var string
*
* @ORM\Column(name="country", type="string", length=40, nullable=false)
*/
private $country;
/**
* @var string
*
* @ORM\Column(name="telephone", type="string", length=20, nullable=true)
*/
private $telephone;
/**
* @var string
*
* @ORM\Column(name="fax", type="string", length=30, nullable=true)
*/
private $fax;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=40, nullable=true)
*/
private $email;
/**
* @var string
*
* @ORM\Column(name="contactNo", type="string", length=20, nullable=false)
*/
private $contactno;
/**
* @var string
*
* @ORM\Column(name="addedBy", type="string", length=100, nullable=true)
*/
private $addedby;
/**
* @var \DateTime
*
* @ORM\Column(name="added", type="datetime", nullable=true)
*/
private $added;
/**
* @var \DateTime
*
* @ORM\Column(name="updated", type="datetime", nullable=false)
*/
private $updated;
/**
* @var integer
*
* @ORM\Column(name="inventLocId", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $inventlocid;
/**
* Set locname
*
* @param string $locname
* @return InventoryLocation
*/
public function setLocname($locname)
{
$this->locname = $locname;
return $this;
}
/**
* Get locname
*
* @return string
*/
public function getLocname()
{
return $this->locname;
}
/**
* Set address1
*
* @param string $address1
* @return InventoryLocation
*/
public function setAddress1($address1)
{
$this->address1 = $address1;
return $this;
}
/**
* Get address1
*
* @return string
*/
public function getAddress1()
{
return $this->address1;
}
/**
* Set address2
*
* @param string $address2
* @return InventoryLocation
*/
public function setAddress2($address2)
{
$this->address2 = $address2;
return $this;
}
/**
* Get address2
*
* @return string
*/
public function getAddress2()
{
return $this->address2;
}
/**
* Set address3
*
* @param string $address3
* @return InventoryLocation
*/
public function setAddress3($address3)
{
$this->address3 = $address3;
return $this;
}
/**
* Get address3
*
* @return string
*/
public function getAddress3()
{
return $this->address3;
}
/**
* Set city
*
* @param string $city
* @return InventoryLocation
*/
public function setCity($city)
{
$this->city = $city;
return $this;
}
/**
* Get city
*
* @return string
*/
public function getCity()
{
return $this->city;
}
/**
* Set zipcode
*
* @param string $zipcode
* @return InventoryLocation
*/
public function setZipcode($zipcode)
{
$this->zipcode = $zipcode;
return $this;
}
/**
* Get zipcode
*
* @return string
*/
public function getZipcode()
{
return $this->zipcode;
}
/**
* Set state
*
* @param string $state
* @return InventoryLocation
*/
public function setState($state)
{
$this->state = $state;
return $this;
}
/**
* Get state
*
* @return string
*/
public function getState()
{
return $this->state;
}
/**
* Set country
*
* @param string $country
* @return InventoryLocation
*/
public function setCountry($country)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* @return string
*/
public function getCountry()
{
return $this->country;
}
/**
* Set telephone
*
* @param string $telephone
* @return InventoryLocation
*/
public function setTelephone($telephone)
{
$this->telephone = $telephone;
return $this;
}
/**
* Get telephone
*
* @return string
*/
public function getTelephone()
{
return $this->telephone;
}
/**
* Set fax
*
* @param string $fax
* @return InventoryLocation
*/
public function setFax($fax)
{
$this->fax = $fax;
return $this;
}
/**
* Get fax
*
* @return string
*/
public function getFax()
{
return $this->fax;
}
/**
* Set email
*
* @param string $email
* @return InventoryLocation
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set contactno
*
* @param string $contactno
* @return InventoryLocation
*/
public function setContactno($contactno)
{
$this->contactno = $contactno;
return $this;
}
/**
* Get contactno
*
* @return string
*/
public function getContactno()
{
return $this->contactno;
}
/**
* Set addedby
*
* @param string $addedby
* @return InventoryLocation
*/
public function setAddedby($addedby)
{
$this->addedby = $addedby;
return $this;
}
/**
* Get addedby
*
* @return string
*/
public function getAddedby()
{
return $this->addedby;
}
/**
* Set added
*
* @param \DateTime $added
* @return InventoryLocation
*/
public function setAdded($added)
{
$this->added = $added;
return $this;
}
/**
* Get added
*
* @return \DateTime
*/
public function getAdded()
{
return $this->added;
}
/**
* Set updated
*
* @param \DateTime $updated
* @return InventoryLocation
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* @return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Get inventlocid
*
* @return integer
*/
public function getInventlocid()
{
return $this->inventlocid;
}
/**
* @var integer
*/
private $id;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @ORM\PrePersist
*/
public function setBeforeInsertData() {
$this->added = new \DateTime();
//$this->updated = new \DateTime();
}
}
您的实体类应该有以下注释:@ORM\HasLifecycleCallbacks()
。您的方法应该具有@ORM\PrePersist
注释。更多信息:向您展示实体我猜您的列名和实体属性不匹配实体完全匹配…您确定使用了正确的注释吗?@bartek稍等,我将用实体更新问题Code@bartek在添加相应的注释后,注释7中是否有需要修改的内容注释给出的错误与“完整性约束冲突:1048列“已更新”不能为空”相同。是否有其他方法使更新的列获得默认值“0000-00-00 00:00:00”,请您通过粘贴完整的实体类(包括所有必要的注释)来更新您的问题?顺便说一句,在做了更改之后,您是否尝试过。/app/console c:c?我可能错了,但注释的代码不起作用。-//$此->updated=new\DateTime()@bartek:Legend说它没有:)您可以尝试一些简单的方法,比如在这个PrePersist()方法中抛出一个异常,看看它是否实际执行了吗?