Welcome Guest, Not a member yet? Register   Sign In
Auto create date - annoation - Codeigniter 2 and Doctrine 2
#1

[eluser]Mario Ropič[/eluser]
Hello!

I am using Doctrine 2 with Codeigniter 2 and I would like to Doctrine automatically generate current date on insert in a given field in the table.

CLASS FILE:
Code:
<?php
namespace models;

/**
* @Entity
* @Table(name="workers")
*/
class Workers {
    /**
     * @Id
     * @Column(type="integer", nullable=false)
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Column(type="string", length=255, unique=true, nullable=false)
     */
    protected $email;

    /**
     * @var datetime $created_on
     *
     * @gedmo:Timestampable(on="create")
     * @Column(type="datetime")
     */
    protected $created_on;


    /** @PrePersist */
    function onPrePersist()
    {
        $this->created_on = date('Y-m-d H:i:s');
    }

    /* Setters & Getters */
    public function setEmail($email){ $this->email = $email; }
    public function getEmail(){ return $this->email; }
}


INSERT METHOD:
Code:
$worker = new models\Workers();
$worker->setEmail($data['email']);
$this->em->persist($worker);
$this->em->flush();


Everytime I insert new record in table "workers", there is allways "created_on" field NULL instead of insertion date. What am I doing wrong?

This question is also on Stack Overflow.




Theme © iAndrew 2016 - Forum software by © MyBB