CodeIgniter Forums
Auto create date - annoation - Codeigniter 2 and Doctrine 2 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Auto create date - annoation - Codeigniter 2 and Doctrine 2 (/showthread.php?tid=46385)



Auto create date - annoation - Codeigniter 2 and Doctrine 2 - El Forum - 10-30-2011

[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.