sjaakp / yii2-novelty
Property indicating novelty of Yii2 ActiveRecord.
Installs: 50
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Type:yii2-extension
pkg:composer/sjaakp/yii2-novelty
Requires
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2025-09-29 02:34:22 UTC
README
Property indicating novelty of Yii2 ActiveRecord
yii2-novelty is a behavior
that adds a property with the name 'novelty' to an ActiveRecord
in the Yii2 PHP framework. This property has one of
the following three values:
'new'in case the record is created since the user visited the site previously;'updated'in case the record is updated since the user visited the site previously;nullin other cases.
The class NoveltyBehavior extends om Yii's TimestampBehavior.
The value of novelty is based on the attribute values of TimestampBehavior, usually called
'created_at' and 'updated_at', and on the time the user visited the site previously.
The previous visit time is stored in two cookies.
Installation
Install yii2-novelty in the usual way with Composer.
Add the following to the require section of your composer.json file:
"sjaakp/yii2-novelty": "*"
or run:
composer require sjaakp/yii2-novelty
You can manually install yii2-novelty by downloading the source in ZIP-format.
Using NoveltyBehavior
Add NoveltyBehavior to your ActiveRecord like this:
<?php
use sjaakp\novelty\NoveltyBehavior;
class MyRecord extends ActiveRecord
{
public function behaviors( ) {
return [
[
'class' => NoveltyBehavior::class,
// ... options ...
],
// ... more behaviors ...
];
}
...
}
NoveltyBehavior extends on yii\behaviors\TimestampBehavior, so you shouldn't use them
together.
After adding NoveltyBehavior the ActiveRecord has an extra property 'novelty', which can be read
like any other property, f.i. with:
$novelty = $record->novelty;
It is a read-only property, so it cannot be written to.
Options
In most cases, NoveltyBehavior will work out of the box. The following options are available for finetuning. All are optional.
- noveltyAttribute
stringName of the read-only attribute. Default:'novelty'. - visitCookie
stringName of the cookie storing the previous visit time. Default:'visit'. - visitStamina
integerExpiration time of the visit-cookie in seconds. Default:31536000(one year). - cacheCookie
stringName of the cookie caching the previous visit time. Default:'visit-cache'. - cacheStamina
integerExpiration time of the cache-cookie in seconds. Default:1800(30 minutes). - format
null|stringPHPdate()format of the TimestampBehavior attributes. Ifnull, this will be set with the right values forvalueis null, or forvalueset tonew Expression('NOW()'), i.e. virtually all use cases. Only in really exotic cases might this be set to anything else thannull. Default:null. - noveltyValues
arrayPossible values assigned tonovelty. Default: see source.
NoveltyBehavior also inherits all the options of yii\behaviors\TimestampBehavior.