yii2-datetime

Date and time behaviors and helpers for Yii2 framework

  • 所有者: omnilight/yii2-datetime
  • 平台:
  • 许可证:
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

Date and Time behaviors for Yii 2

This extension helps you to deal with date and time attributes of the models for Yii framework 2.0

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist omnilight/yii2-datetime "*"

or add

"omnilight/yii2-datetime": "*"

to the require section of your composer.json.

Why?

When working with models and forms it is often needed to provide user a way to edit attributes that holds
date and/or time. In this case typical problems are:

  1. Date/time formats could be different in the database and in the displayed form (due to local settings, for ex.)
  2. You should validate values entered by the user

So you have to set correct format for database, form, somehow convert them from one to another, and it would be
nice if you can setup correct formats once (on application level) and lately do not worry about them. This extension
helps is this area.

The idea

Idea is to have separate attribute for each editable date/time property of the model, that will be used in the form.
This attribute will not be stored in the DB, but it will be used to present user with correctly formatted value.
And when you assign value to this attribute, it will be automatically converted and assigned to the DB property of
the model.

This extension provides special behavior, that automates work with this attributes

How to use

In your model:

/**
 * @property string posted_at This is your property that you have in the database table, it has DATETIME format
 */
class Post extends ActiveRecord
{
    // ... Some code here

    public function behaviors()
    {
        return [
            'datetime' => [
                'class' => DateTimeBehavior::className(), // Our behavior
                'attributes' => [
                    'posted_at', // List all editable date/time attributes
                ],
            ]
        ];
    }
}

Now in your view with the form:

// $model has instance of Post
<?= $form->field($model, 'posted_at_local')->widget(\yii\jui\DatePicker::className(), \omnilight\datetime\DatePickerConfig::get($model, 'posted_at_local')) ?>
// DatePickerConfig is used to properly configure widget. Currently it only supports DatePicker from the Jui extension

That's all! User will enter date in the his local format and will be converted to the database automatically.

How is works

Behavior creates "virtual" attribute named attribute_name_local for each attribute you define in the 'attributes' section.
When you read $yourModel->attribute_name_local behavior will return object with the type DateTimeAttribute. If
this object will be used in the string context, it will be converted to string with the magical __toString method.
And during this original value of attribute_name will be converted into the local representation.

When you assign value to the $yourModel->attribute_name_local internally it will be assigned to value property
of the DateTimeAttribute class and converted to the your database-stored property.

You can also define individual configuration for each attribute and define it's local name, format and so on.

主要指标

概览
名称与所有者omnilight/yii2-datetime
主编程语言PHP
编程语言PHP (语言数: 1)
平台
许可证
所有者活动
创建于2014-09-18 08:50:11
推送于2017-02-24 14:13:34
最后一次提交2016-09-04 14:56:51
发布数3
最新版本名称1.1.1 (发布于 )
第一版名称1.0.0 (发布于 )
用户参与
星数9
关注者数4
派生数5
提交数24
已启用问题?
问题数3
打开的问题数1
拉请求数1
打开的拉请求数0
关闭的拉请求数1
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?