bahirul / yii2-autonumber
Auto number extension for the Yii framework + Enhacement
Installs: 320
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 18
Type:yii2-extension
Requires
- yiisoft/yii2: ~2.0
README
Yii2 extension to genarete formated autonumber. It can be used for generate document number.
This extension forked from mdm/yii2-autonumber with some mods.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist bahirul/yii2-autonumber "~1.0"
or add
"bahirul/yii2-autonumber": "~1.0"
to the require section of your composer.json
file.
Usage
Prepare required table by execute yii migrate.
yii migrate --migrationPath=@bahirul/yii2/autonumber/migrations
if wantn't use db migration. you can create required table manually.
CREATE TABLE auto_number ( "group" varchar(32) NOT NULL, "number" int, optimistic_lock int, update_time int, PRIMARY KEY ("group") );
Once the extension is installed, simply modify your ActiveRecord class:
public function behaviors() { return [ [ 'class' => 'bahirul\yii2\autonumber\Behavior', 'attribute' => 'sales_num', // required 'group' => $this->id_branch, // optional 'value' => 'SA.'.date('Y-m-d').'.?' , // format auto number. '?' will be replaced with generated number or you can use " 'value' => function($event){ return 'SA.'.date('Y-m-d').'.?' } " as long the return value contain '?' character 'digit' => 4 // optional, default to null. ], ]; } // it will set value $model->sales_num as 'SA.2014-06-25.0001'
Instead of behavior, you can use this extension as validator
public function rules() { return [ [['sales_num'], 'autonumber', 'format'=>'SA.'.date('Y-m-d').'.?'], ... ]; }