PHP code example of antonyz89 / change-log-behavior
1. Go to this page and download the library: Download antonyz89/change-log-behavior library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
antonyz89 / change-log-behavior example snippets
public function behaviors()
{
return [
...
[
'class' => ChangeLogBehavior::className(),
'db' => Yii::$app->other_db, # optional (default is the same as Yii::$app->db)
'excludedAttributes' => ['updated_at'],
],
...
];
}
public function behaviors()
{
return [
[
'class' => ChangeLogBehavior::class,
'autoCache' => true,
'customFields' => [
'total' => static function (self $model) {
return $model->calculateTotal();
},
// or static function (self $model) { return $model->createdBy->name; }
'created_by' => 'createdBy.name'
]
]
];
}
public function behaviors()
{
return [
[
'class' => ChangeLogBehavior::class,
'autoCache' => true,
'customFields' => [
'total' => static function (self $model) {
return $model->calculateTotal();
},
// `user_id` will be registered even if it hasn't changed
'user_id!' => 'user.name',
'created_by' => 'createdBy.name'
]
]
];
}
class FooController extends Controller {
// ...
public function actionUpdate($id) {
$model = $this->findModel($id);
// cache custom fields manually
// [[cacheCustomFields()]] is a magic method that calls [[ChangeLogBehavior::cacheCustomFields()]]
$model->cacheCustomFields();
$modelChildren = array_map(function () {
// imagine something cool here
}, $this->request->post());
foreach ($modelChildren as $modelChild) {
$modelChild->parent_id = $model->id;
$modelChild->save();
}
// on save the custom fields are computed again and saved if they changed
$model->save();
}
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => ChangeLogBehavior::class,
'dataOnDelete' => true
]
];
}
public function behaviors()
{
return [
[
'class' => ChangeLogBehavior::class,
// get `user_id` from model ($model->user_id)
'parentId' => 'user_id',
// get `user_id` from model using static function
'parentId' => static functiobn (self $model) {
if ($model->type !== 'ADMIN')
return $model->user_id;
}
return null;
]
];
}
/**
* @propertu int id
* @property int created_at
* @property int updated_at
* @property string title
* @property int rating
*/
class Post extends yii\db\ActiveRecord {
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => ChangeLogBehavior::class,
'excludedAttributes' => ['created_at','updated_at'],
// (optional) autoCache is disabled by default
'autoCache' => false,
// (optional) - custom fields
'customFields' => [
'total' => static function (self $model) {
return $model->calculateTotal();
},
// or static function (self $model) { return $model->createdBy->name; }
'created_by' => 'createdBy.name'
]
]
];
}
}
use antonyz89\ChangeLogBehavior\ListWidget as ChangeLogList;
use app\models\Post;
/**
* @var Post $model
*/
echo DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'title',
'rating',
'created_at:datetime',
'updated_at:datetime',
],
]);
echo ChangeLogList::widget([
'db' => Yii::$app->other_db, # optional (default is the same as Yii::$app->db)
'model' => $model,
]);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.