We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to specify a column type datetime?.

Hi. I am sending to save a date in my database,

02/08/2016 07:13:47

but I have the following error

    SQLSTATE[HY000]: General error: 1843 OCIStmtExecute: ORA-01843: not a valid month        (/tmp/pear/download/PDO_OCI-1.0/oci_statement.c:142)

IF I run the statement directly to the database, it is stored correctly but I check the model and find that this type column string

/**
 *
 * @var string
 */
public $DETA_FECHA; 

How to specify a column type datetime?. Here is my model

<?php
use Phalcon\Mvc\Model\Behavior\Timestampable;
class SPTDETALLE extends \Phalcon\Mvc\Model{
    /**
     *
     * @var string
     */
    public $DETA_CODIGO;
    /**
     *
     * @var string
     */
    public $DETA_CANTID;
    /**
     *
     * @var string
     */
    public $DETA_FECHA; 
    /**
     * Initialize method for model.
     */
    public function initialize()    {
        $this->setSchema("SPOLS");
         $this->addBehavior(new Timestampable([
        'beforeSave'  => [
            'field'     => 'DETA_FECHA',
             'format'    => 'd/m/Y H:i:s'     ]
    ]));
    }
    public function getSource()    {
        return 'SPT_DETALLE';
    }
    public static function find($parameters = null)    {
        return parent::find($parameters);
    }
    public static function findFirst($parameters = null)    {
        return parent::findFirst($parameters);
    }
}

The standard DateTime class cannot be converted to string. Create your own class:

class MyDateTime extends \DateTime
{
    public function __toString() {
        return $this->format("d/m/y H:i:s");
    }
}
$model = new SPTDETALLE();
$model->DETA_FECHA = new MyDateTime();