Giter Site home page Giter Site logo

yii2-behaviors's Introduction

Yii2 behaviors

Collection of useful behaviors for Yii Framework 2.0

Latest Stable Version Total Downloads License Build Status

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yii2mod/yii2-behaviors "*"

or add

"yii2mod/yii2-behaviors": "*"

to the require section of your composer.json file.

Usage

  1. PurifyBehavior
    public function behaviors()
    {
        return [
            'purify' => [
                'class' => PurifyBehavior::className(),
                'attributes' => ['title', 'content'],
                'config' => [
                    'AutoFormat.Linkify' => true,
                    'HTML.TargetBlank' => true,
                    'HTML.Nofollow' => true
                ]
            ]
        ];
    }
  1. CarbonBehavior

CarbonBehavior automatically creates a Carbon Instance for one or multiple attributes of an ActiveRecord object when afterFind event happen.

    public function behaviors()
    {
            return [
                'carbon' => [
                    'class' => CarbonBehavior::className(),
                    'attributes' => [
                        'createdAt',
                        'trialEndAt',
                    ]
                ],
            ];
     }

      $user = UserModel::findOne(1);

      var_dump($user->createdAt->year); // 2016
      var_dump($user->createdAt->month); // 5
      var_dump($user->createdAt->day); // 10

      // change date
      
      $user->trialEndAt->addYear();
      $user->save();

Carbon Documentation

Support us

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

yii2-behaviors's People

Contributors

dmitry-semenov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

yii2-behaviors's Issues

CarbonBehavior: Problem with yii\db\Expression

Hi I have tested your CarbonBehavior and would find it quite usefull.

But i have an issue when saving new db entries.

In my Model i have this behavior settting:

 [
            'class' => TimestampBehavior::className(),
            'attributes' => [
                ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
            ],
            'value' => new yii\db\Expression('UTC_TIMESTAMP()'),
  ],
  'carbon' => [
            'class' => CarbonBehavior::className(),
            'attributes' => [
                'created_at',
                'updated_at',
            ]
        ],

When i now save an model e.g. the updated_at value is a db expression, which yours CarbonBehavior fails to accept.

This is what throws the exception

InvalidArgumentException
A four digit year could not be found
Data missing.

call_user_func([yii2mod\behaviors\CarbonBehavior, 'attributesToCarbon'], yii\db\AfterSaveEvent) 
yii2mod\yii2-behaviors\CarbonBehavior.php at line 105 โ€“ Carbon\Carbon::createFromFormat('Y-m-d H:i:s', yii\db\Expression) 

I there a possibility to disable the AfterSaveEvent ?
How do you set your created_at/ updated_at on beforeInsert/beforeUpdate?
do you just use Carbon\Carbon::now() ?

thanks in advance

Initially dirty attributes

Carbonified attributes get dirty right after find:

$model = SomeModel::find()->one();
$model->getDirtyAttributes(); // got list of Carbon attributes here

My suggestion - rewrite oldAttribute after every conversion:

$this->owner->$attribute = Carbon::createFromTimestamp($value);
$this->owner->setOldAttribute($attribute, $this->owner->$attribute);

issues with different phpversions

Hi i have issues with different behaviour of the Carbonbehaviour when using different php versions.

Background: i have a wamp installation where i can easily switch between different php versions.
All have the same php.ini settings and also the application is the same.

I am using the CarbonBehavior as followed in my Model class:

public function behaviors()
{
    $behaviors = parent::behaviors();
    $behaviors['carbon'] = [
            'class' => CarbonBehavior::className(),
            'attributes' => [
                'review_start',
                'review_end',
                'crf_review_start',
                'crf_review_end',
                'created_at',
                'updated_at',
            ]
        ];
    return $behaviors;
}

The columns are all of type datetime.

I have different "output" of the attributes depending on the php version I use.

PHP 7.1.27:

$this->crf_review_end
Carbon\Carbon#1
(
    [date] => '2019-03-13 07:55:12.000000'
    [timezone_type] => 3
    [timezone] => 'UTC'
)
$this->crf_review_end->utc
    true

$this->crf_review_end->local
   true

date_default_timezone_get()
   'UTC'

PHP 7.2.16 + PHP 7.3.3:

$this->crf_review_end
Carbon\Carbon#1
(
    [date] => '2019-03-13 08:55:12.000000'
    [timezone_type] => 3
    [timezone] => 'Europe/Berlin'
)
$this->crf_review_end->utc
    false

$this->crf_review_end->local
    false

date_default_timezone_get()
   'UTC'

Without Carbon (Stored the UTC value)

 $this->crf_review_end
 '2019-03-13 07:55:12'

Displayed on the widget formatted into the Berlin Timezone => Mar 13, 2019 8:55:12 AM

As the php settings are all the same i don't know where the Problem could be.

Is there a reason that the Carbon extensions used is limited to "nesbot/carbon": "~1.0" in the composer.json file?

The installed version is of the "nesbot/carbon" is 1.36.2 where the latest version is 2.16.0.

from php.ini:

Timezone Database | internal
Default timezone | UTC
date.timezone | UTC
$_SERVER['FORMATER_DEFAULT_TIMEZONE'] | UTC
$_SERVER['FORMATER_TIMEZONE'] | Europe/Berlin

As I understand Carbon 1.x should be used for php versions < 7.1 and Carbon 2 for >= 7.1.
But your behavior only uses the Carbon 1 version.

"Carbon 2 (requiring PHP >= 7.1) perfectly supports microseconds. But if you use Carbon 1 and PHP < 7.1, read our section about partial microseconds support."
https://carbon.nesbot.com/docs/

Sadly the Carbon 1 Version doesn't seem to work correctly for php >= 7.2.
Do you have an idea how this can be fixed?

PHP 8.0 support?

Hi!,

Thanks for this great project!

Will be any update to support PHP 8.0?

Many thanks!

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.