Giter Site home page Giter Site logo

yii2-format-converter's Introduction

yii2-format-converter

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require mdmsoft/yii2-format-converter "~1.0"

or add

"mdmsoft/yii2-format-converter": "~1.0"

to the require section of your composer.json file.

Usage

Once the extension is installed, simply modify your ActiveRecord class:

DateConverter Behavior

Use to convert date format from database format to logical format

public function behaviors()
{
    return [
        [
            'class' => 'mdm\converter\DateConverter',
            'type' => 'date', // 'date', 'time', 'datetime'
            'logicalFormat' => 'php:d/m/Y', // default to locale format
            'physicalFormat' => 'php:Y-m-d', // database level format, default to 'Y-m-d'
            'attributes' => [
                'Date' => 'date', // date is original attribute
            ]
        ],
        ...
    ]
}

then add attribute Date to your model rules.

// in view view.php
echo DetailView::widget([
	'options' => ['class' => 'table table-striped detail-view', 'style' => 'padding:0px;'],
	'model' => $model,
	'attributes' => [
		'sales_num',
		'supplier.name',
		'Date', // use attribute 'Date' instead of 'sales_date'
		'nmStatus',
	],
]);


// in view _form.php 
echo $form->field($model, 'Date')
	->widget('yii\jui\DatePicker', [
		'options' => ['class' => 'form-control', 'style' => 'width:50%'],
		'dateFormat' => 'php:d/m/Y', 
]);

RelatedConverter Behavior

Convert id to name of related model

// attach to model
public function behaviors()
{
    return [
        [
            'class' => 'mdm\converter\RelatedConverter',
            'attributes => [
                'supplierName' => ['supplier', 'name'], // use avaliable relation
                'branchName' => [[Branch::className(), 'id' => 'branch_id'], 'name'], // use classname
            ]
        ],
    ];
}

// usage
$model->supplierName = 'Donquixote Family';
$model->branchName = 'North Blue';

// in form
<?= $form->field($model,'supplierName'); ?>

EnumConverter Behavior

Use to convert constant value to constant name.

class Post extends ActiveRecord
{
    const STATUS_DRAFT = 1;
    const STATUS_PUBLISHED = 2;
    const STATUS_DELETED = 3;

    ...

    public function behaviors()
    {
        return [
            [
                'class' => 'mdm\converter\EnumConverter',
                'attributes => [
                    'statusName' => 'status', // 
                ],
                'prefix' => 'STATUS_'
            ],
        ];
    }
}

// usage
$model->status = Post::STATUS_PUBLISHED;

echo $model->statusName; // return Published

EnumTrait

Use to get list of constant

class Post extends ActiveRecord
{
    use \mdm\converter\EnumTrait;
    
    const STATUS_DRAFT = 1;
    const STATUS_PUBLISHED = 2;
    const STATUS_DELETED = 3;

    public function getNmStatus()
    {
        return $this->getLogical('status', 'STATUS_');
    }
    
    public function setNmStatus($value)
    {
        return $this->setLogical('status', 'STATUS_', $value);
    }
    
}

// usage
$model->nmStatus = 'DRAFT'; // eq $model->status = 1;

$model->status = 2;
echo $model->nmStatus; // return PUBLISHED;

Post::enums('STATUS_');
/*
[
    1 => 'DRAFT',
    2 => 'PUBLISHED',
    3 => 'DELETED',
]
*/ 

Post::constants('STATUS_');
/*
[
    'DRAFT' => 1,
    'PUBLISHED' => 2,
    'DELETED' => 3,
]
*/ 

yii2-format-converter's People

Contributors

mdmunir avatar zqrt avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.