Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6914515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:21:26+00:00 2026-05-27T09:21:26+00:00

I am using Doctrine 2 with Codeigniter 2 and I would like to Doctrine

  • 0

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:

<?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:

$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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:21:26+00:00Added an answer on May 27, 2026 at 9:21 am

    If i am wrong, correct me please. But i knew as if Doctrine does not support default. You do that in php level like

    /**
     * @Column(type="string", length=255)
     */
    private $something = "blabla";
    

    Looking at your source code, i see that you are using gedmo extension for doctrine. Am i right? So you have got two ways to do this.

    1)Just using Doctrine, No Gedmo

    Read this manual very carefully and you will notice @HasLifecycleCallbacks Annotations.

    So you should edit your code as;

    CLASS FILE

    <?php 
      namespace models;
    
     /**
      * @Entity
      * @Table(name="workers")
      * @HasLifecycleCallbacks
      */
    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
      * @Column(type="datetime")
      */
    protected $created_on;
    
     /** @PrePersist */
     function onPrePersist()
     {
         //using Doctrine DateTime here
         $this->created_on = new \DateTime('now');
     }
    
     /* Setters & Getters */
     public function setEmail($email){ $this->email = $email; }
     public function getEmail(){ return $this->email; }
    }
    

    2)Using Gedmo

    If you prefer using Gedmo Timestampable extension, then just drop the function prepersist, cause gedmo is doing everything for you. I also checked my source codes. I hope i do not have wrong predictions here

    CLASS FILE

    <?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
      * @Column(type="datetime")
      * @gedmo:Timestampable(on="create")
      */
    protected $created_on;
    
     /* Setters & Getters */
     public function setEmail($email){ $this->email = $email; }
     public function getEmail(){ return $this->email; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im using symfony with doctrine, and Im trying to generate the schema yml file
I am using Doctrine with Symfony2. My config.yml file looks something like this:- Doctrine
Is there any way, when using Doctrine, to create a table like another? I
I'm using Doctrine with Codeigniter. So i write a library class for using them
I am getting following error message when using Doctrine ORM in Codeigniter. ( !
I'm using symfony with doctrine and I make a registration form. Email field is
I'm using Doctrine with Zend Framework. For my model, I'm using a base class,
I'm using Codeigniter and Doctrine together for a project. I've gotten everything set up
I have been using Codeigniter with Doctrine 1.2.3 and I am wondering is it
I'm using Doctrine DBAL and wanting to do an INSERT ... ON DUPLICATE UPDATE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.