在MySQL中插入数据时,Symfony2.4,Doctrine,PHP中的更新列会出现错误

时间:2014-05-28 11:20:21

标签: php symfony doctrine

问题 - 我想保存从表单提交的数据,当我将其保存到MySQL时收到错误 -

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'updated' cannot be null 

但是我已经将更新列的默认值设置为0000-00-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();
    }
}

先谢谢。

1 个答案:

答案 0 :(得分:0)

您的实体类应具有以下注释:@ORM\HasLifecycleCallbacks()。您的方法应该有@ORM\PrePersist注释。更多信息:http://symfony.com/doc/current/book/doctrine.html#lifecycle-callbacks

相关问题