Automatically select dates from databases as Zend_Date objects

From what I understand, the best way to handle dates in the Zend Framework is to select them as a Unix timestamp from the database.

Quickly create dates from database date values

// SELECT UNIX_TIMESTAMP(my_datetime_column) FROM my_table
$date = new Zend_Date($unixtimestamp, Zend_Date::TIMESTAMP);

I think the pain is that there is no easy way in Oracle to select dates as Unix timestamps or in ISO-8601 format that best fit the two formats Zend_Date.

But I wrote a function to select dates as unix timestamps in PL / SQL, so I can really do it now.

Using Zend_Db_Expr, now I can select my dates as Unix timestamps:

$select = $db->select()
             ->from(array('p' => 'products'),
                    array(
                        'product_id',
                        'product_date' => new Zend_Db_Expr('toUnixTimestamp(product_date)')
                    )
               );

$results = $db->fetchAll($select);

You would use a similar query for any RDMS - most of them have a timestamp function.

, $results, Zend_Date :

foreach($results as $result){
    $productDate = new Zend_Date($result['product_date'], Zend_Date::TIMESTAMP);
    echo $productDate->toString('dd/MMM/yyyy HH:mm:ss');
}

, $results, Zend_Date. , .

, :

- Zend_Db, - - , Zend_Date?

+3
2

, . , :

  • Zend_Db_Table_Row - __get() __set()
  • /, ,

, :

/**
 * @category   FireUp
 * @package    FireUp_Db
 * @copyright  Copyright (c) 2007-2009 Fire Up Media, Inc. (http://www.fireup.net)
 * @license    http://dev.fireup.net/license/mit     MIT License
 * @uses       Zend_Db_Table_Row
 */
class FireUp_Db_Table_Row extends Zend_Db_Table_Row_Abstract
{
    /**
     * Retrieve row field value
     *
     * Checks for the existence of a special method to apply additional handling for the field data and calls the method if it exists
     *
     * @param  string $columnName The user-specified column name.
     * @return string             The corresponding column value.
     * @throws Zend_Db_Table_Row_Exception if the $columnName is not a column in the row.
     */
    public function __get($key)
    {
        $inflector = new Zend_Filter_Word_UnderscoreToCamelCase();

        $method = '_get' . $inflector->filter($key);

        if (method_exists($this, $method))
        {
            return $this->{$method}();
        }

        return parent::__get($key);
    }

    /**
     * Set row field value
     *
     * Checks for the existence of a special method to apply additional handling for the field data and calls the method if it exists
     *
     * @param  string $columnName The column key.
     * @param  mixed  $value      The value for the property.
     * @return void
     * @throws Zend_Db_Table_Row_Exception
     */
    public function __set($key, $value)
    {
        $inflector = new Zend_Filter_Word_UnderscoreToCamelCase();

        $method = '_set' . $inflector->filter($key);

        if (method_exists($this, $method))
        {
            return $this->{$method}($value);
        }

        return parent::__set($key, $value);
    }
}

:

class EntityRecord extends FireUp_Db_Table_Row
{
    protected function _getDateCreated()
    {
        return new Zend_Date($this->_data['date_created'], Zend_Date::ISO_8601);
    }

    protected function _setDateCreated($value)
    {
        if ($value instanceof Zend_Date)
        {
            $value = $value->toString('YYYY-MM-dd HH:mm:ss');
        }

        $this->_data['date_created'] = $value;
        $this->_modifiedFields['date_created'] = true;
    }
}

Zend_Date , .., , .

+6

Zend_Table , Zend_Db_Table_Row_Abstract. ,

function getDate() {
    return new Zend_Date($this->datecol, Zend_Date::TIMESTAMP);
}
0

Source: https://habr.com/ru/post/1721457/


All Articles