kcdsh
kcdsh

Reputation: 59

Symfony Doctrine PrePersist/PreUpdate not working

I'm having some problems with lifecyclecallbacks in symfony. Here's my entity:

<?php

namespace MainBundle\Bundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use AppBundle\Entity\Event;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * User
 *
 * @ORM\Table("users")
 * @ORM\Entity(repositoryClass="MainBundle\Bundle\Repository\UserRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class User extends BaseUser
{

    public function __construct() {
        $this->events = new ArrayCollection();
        parent::__construct();
    }

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

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

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="updated", type="datetime",nullable=true)
     */
    private $updated;

    /**
     * events
     *
     * @var ArrayCollection
     * @access private
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Event",mappedBy="user")
     */
    private $events;


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

    /**
     * Set joined
     *
     * @param \DateTime $joined
     * @return User
     */
    public function setJoined($joined)
    {
        $this->joined = $joined;

        return $this;
    }

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

    /**
     * Set updated
     *
     * @param \DateTime $updated
     * @return User
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;

        return $this;
    }

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

    /**
     * Set events
     *
     * @param \DateTime $events
     * @return User
     */
    public function setEvents($events)
    {
        $this->events = $events;

        return $this;
    }

    /**
     * Get events
     *
     * @return ArrayCollection 
     */
    public function getEvents()
    {
        return $this->events;
    }

    /**
     * Add event
     *
     * @param Event $event
     * @return User
     */
    public function addEvent(Event $event)
    {
        $this->events->add($event);

        return $this;
    }

    /**
     * Remove event
     *
     * @param Event $event
     * @return User
     */
    public function removeEvent(Event $event)
    {
        $this->events->removeElement($event);

        return $this;
    }

    /*
     * @ORM/PrePersist
     */
    public function PrePersist()
    {
        $this->joined = new \DateTime();
    }

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

I've been searching for a while now, and still haven't spotted the issue. I've tried some variations of what's here e.g. experiments with case-sensitive rules(I mean using upper/lower cases here and there around those 2 methods) and also I tried to begin block quotes with double star /**(as for this I still don't know whether it works or not). If any more files are needed here, I'll paste them.

Upvotes: 2

Views: 3037

Answers (1)

Darragh Enright
Darragh Enright

Reputation: 14136

You appear to have some typos. Instead of:

 @ORM/PrePersist

try:

 @ORM\PrePersist

e.g:

/**
 * @ORM\PrePersist
 */
public function PrePersist()
{
    $this->joined = new \DateTime();
}

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

Surprised this wasn't throwing an exception, but there you go. Hope this helps! :)

Upvotes: 1

Related Questions