【问题标题】:Auto create date - annoation - Codeigniter 2 and Doctrine 2自动创建日期 - 注释 - Codeigniter 2 和 Doctrine 2
【发布时间】:2011-10-29 12:05:28
【问题描述】:

我将 Doctrine 2 与 Codeigniter 2 一起使用,我希望 Doctrine 在插入表格中的给定字段时自动生成当前日期。

类文件:

<?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; }
}

插入方法:

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

每次我在表“workers”中插入新记录时,总是有created_on 字段NULL 而不是插入日期。我做错了什么?

【问题讨论】:

    标签: codeigniter datetime doctrine automation annotations


    【解决方案1】:

    如果我错了,请纠正我。但我知道 Doctrine 好像不支持默认值。您可以在 php 级别执行此操作,例如

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

    查看您的源代码,我发现您正在使用 gedmo 扩展作为学说。我对吗?所以你有两种方法可以做到这一点。

    1) 只使用 Doctrine,没有 Gedmo
    仔细阅读this manual,您会注意到@HasLifecycleCallbacks 注解。

    所以你应该编辑你的代码;

    类文件

    <?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)使用 Gedmo

    如果您更喜欢使用 Gedmo Timestampable 扩展,那么只需删除函数 prepersist,因为 gedmo 正在为您做所有事情。我还检查了我的源代码。我希望我在这里没有错误的预测

    类文件

    <?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; }
    }
    

    【讨论】:

    • 感谢您的解决方案。我在没有 GEDMO 扩展的情况下解决了.. 谢谢!
    • 我使用这个默认值:@ORM\Column(type="integer", options={"default":1})
    【解决方案2】:

    对于学说 2.3,使用

    /**
     * @var datetime $created_on
     * 
     * @Column(type="datetime", options={"default"="CURRENT_TIMESTAMP"})
     */
    protected $created_on;
    

    【讨论】:

      【解决方案3】:

      您可以尝试以下方法:

      /**
       * @var timestamp $created_on
       * 
       * @Column(type="timestamp", default="CURRENT_TIMESTAMP")
       */
      protected $created_on;
      

      【讨论】:

      • 谢谢。我会尽快解决我的新问题 - stackoverflow.com/questions/7985676/…
      • 当我尝试您的解决方案时,仍然有错误 - pastebin.com/pC313P0S。有什么想法吗?
      • 类型必须是日期时间,虽然它会被应用为默认值 CURRENT_TIMESTAMP 的时间戳...时间戳不是一个有效的类型!
      猜你喜欢
      • 1970-01-01
      • 2011-11-12
      • 2011-10-08
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 2012-10-02
      相关资源
      最近更新 更多