为什么我的PrePersist和PreUpdate返回null?

时间:2015-08-28 14:49:03

标签: php symfony doctrine-orm

在我的Doctrine实体中,我试图使用LifecycleCallbacks。由于某种原因,保持返回null

时间表实体

namespace Petan\LogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Timesheet
 *
 * @ORM\Table(name="timesheet")
 * @ORM\Entity(repositoryClass="Petan\LogBundle\Entity\TimesheetRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Timesheet
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text")
     */
    private $description;

    /**
     * @var string
     *
     * @ORM\Column(name="hours", type="decimal", precision=5, scale=1)
     */
    private $hours;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="worked_at", type="date")
     */
    private $workedAt;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created_at", type="datetime")
     */
    private $createdAt;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="updated_at", type="datetime")
     */
    private $updatedAt;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="started_at", type="time")
     */
    private $startedAt;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="ended_at", type="time")
     */
    private $endedAt;

    /**
     * @var boolean
     *
     * @ORM\Column(name="is_deleted", type="boolean")
     */
    private $isDeleted;


    /**
     * toString
     *
     * @return string
     */
    public function __toString()
    {
        return $this->name;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Timesheet
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Timesheet
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set hours
     *
     * @param string $hours
     * @return Timesheet
     */
    public function setHours($hours)
    {
        $this->hours = $hours;

        return $this;
    }

    /**
     * Get hours
     *
     * @return string 
     */
    public function getHours()
    {
        return $this->hours;
    }

    /**
     * Set workedAt
     *
     * @param \DateTime $workedAt
     * @return Timesheet
     */
    public function setWorkedAt($workedAt)
    {
        $this->workedAt = $workedAt;

        return $this;
    }

    /**
     * Get workedAt
     *
     * @return \DateTime 
     */
    public function getWorkedAt()
    {
        return $this->workedAt;
    }

    /**
     * Set created_at
     *
     * @param \DateTime $createdAt
     * @return Work
     */
    public function setCreatedAt($createdAt)
    {
        $this->created_at = $createdAt;

        return $this;
    }

    /**
     * Get created_at
     *
     * @return \DateTime 
     */
    public function getCreatedAt()
    {
        return $this->created_at;
    }

    /**
     * Set updated_at
     *
     * @param \DateTime $updatedAt
     * @return Work
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updated_at = $updatedAt;

        return $this;
    }

    /**
     * Get updated_at
     *
     * @return \DateTime 
     */
    public function getUpdatedAt()
    {
        return $this->updated_at;
    }

    /**
     * Set startedAt
     *
     * @param \DateTime $startedAt
     * @return Timesheet
     */
    public function setStartedAt($startedAt)
    {
        $this->startedAt = $startedAt;

        return $this;
    }

    /**
     * Get startedAt
     *
     * @return \DateTime 
     */
    public function getStartedAt()
    {
        return $this->startedAt;
    }

    /**
     * Set endedAt
     *
     * @param \DateTime $endedAt
     * @return Timesheet
     */
    public function setEndedAt($endedAt)
    {
        $this->endedAt = $endedAt;

        return $this;
    }

    /**
     * Get endedAt
     *
     * @return \DateTime 
     */
    public function getEndedAt()
    {
        return $this->endedAt;
    }

    /**
     * Set isDeleted
     *
     * @param boolean $isDeleted
     * @return Timesheet
     */
    public function setIsDeleted($isDeleted)
    {
        $this->isDeleted = $isDeleted;

        return $this;
    }

    /**
     * Get isDeleted
     *
     * @return boolean 
     */
    public function getIsDeleted()
    {
        return $this->isDeleted;
    }

    /**
     * @ORM\PrePersist
     */
    public function prePersist() {
        $this->created_at = new \DateTime;
        $this->updated_at = new \DateTime;

        // Set hours
        $this->hours = $this->getDuration();
    }

    /**
     * @ORM\PreUpdate
     */
    public function preUpdate() {
        $this->updated_at = new \DateTime;

        // Set hours
        $this->hours = $this->getDuration();
    }

    /**
     * get Duration (in hours) between work's start-end time
     * @param void
     * @return float
     */
    public function getDuration()
    {

        // Diff in seconds
        $interval = $this->getEndedAt()->getTimestamp() - $this->getStartedAt()->getTimestamp();

        // If value is negative, ended_at is the day after
        // Add a day (in seconds)
        if ($interval < 0)
            $interval += (24*60*60);

        // Returns hours
        return $interval / (60*60);
    }
}

错误

SQLSTATE [23000]:完整性约束违规:1048列&#39; created_at&#39;不能为空

注意:我知道有类似的问题,但他们没有针对这个具体问题。

2 个答案:

答案 0 :(得分:3)

我认为你是使用驼峰和下划线混合属性名称。

答案 1 :(得分:1)

你应该总是使用camelCase作为@kormik提示你

Stof Extensions可能会让您感兴趣:https://github.com/stof/StofDoctrineExtensionsBundle

/**
 * @ORM\PrePersist
 */
public function prePersist() {
    $this->createdAt = new \DateTime;
    $this->updatedAt = new \DateTime;

    //Better using setter : $this->setCreatedAt(new \DateTime());

    // Set hours
    $this->hours = $this->getDuration();
}

/**
 * @ORM\PreUpdate
 */
public function preUpdate() {
    $this->updatedAt = new \DateTime;

    // Set hours
    $this->hours = $this->getDuration();
}