Al insertar los datos en MySQL, se obtiene un error para la columna de actualización en Symfony2.4, Doctrine, PHP

Problem - I want to save the data that is submitted from the form, when i am saving it to MySQL getting error as -

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

But I have given the update column the default value as 0000-00-00 00:00:00 , by default it must take the default value.

I am using the "prepersist" technique to add the code before saving it to MySQL.

The following is my "prepersist" code snippet from which the above error got generated-

public function setBeforeInsertData() {
    $this->added    =   new \DateTime();
}

I have used the following code to overcome the error just for the time being I know that its a wrong approach.

public function setBeforeInsertData() {
    $this->added    =   new \DateTime();
    $this->updated  =   new \DateTime();
}

The following is the Entity for the respective table -

<?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();
    }
}

Gracias de antemano.

preguntado el 28 de mayo de 14 a las 12:05

show you entity i guess you have mismatched column names and properties of entity -

The entities are matching perfectly... -

Are you sure that you used proper annotations? -

@bartek Just a sec I will update question with the Entity Code -

@bartek is there anything that i need to modify in the annotations -

1 Respuestas

Your entity class should have following annotation: @ORM\HasLifecycleCallbacks(). Your methods should have @ORM\PrePersist annotation. More info: http://symfony.com/doc/current/book/doctrine.html#lifecycle-callbacks

contestado el 28 de mayo de 14 a las 12:05

Even after adding the respective annotations its giving the same error as - "Integrity constraint violation: 1048 Column 'updated' cannot be null ". Is there any other way so that the updated column will get the default value as '0000-00-00 00:00:00' - Channaveer Hakari

Would you please be so kind and update your question by pasting FULL entity class including ALL necessary annotations? BTW have you tried ./app/console c:c after doing changes? - Bartek

I might be wrong, but commented code won't work. - //$this->updated = new \DateTime(); - Bartek

@bartek: Legend says it does not :) - Jovan Perović

Can you try something simple like throwing an Exception inside this PrePersist() method to see if it is actually executed? - Zeljko

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.