Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.
/ trash-bin-behavior Public archive

AR models behavior that allows to mark models as deleted and then restore them.

Notifications You must be signed in to change notification settings

yiiext/trash-bin-behavior

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ETrashBinBehavior

Adds an ability to mark model as deleted and restore it when needed.

Installing and configuring

Preparing model

There should be an attribute in the model to store deletion flag. For example, "is_removed".

Attaching behavior

<?php
function behaviors()
{
	return array(
		'trash'=>array(
			'class'=>'ext.yiiext.behaviors.model.trashBin.ETrashBinBehavior',
			// Deletion flag table column name (required)
			'trashFlagField'=>'is_removed',
			// Value that is written to $trashFlagField when model is deleted
			// Default is 1
			'removedFlag'=>1,
			// Value that is written to $trashFlagField when model is restored
			// Default is 0
			'restoredFlag'=>0,
		),
	);
}

Requirements

Yii Framework 1.1.4

API

remove()

Mark model as deleted.

<?php
$user=User::model()->findByPk(1);
$user->remove();
$user->save();

restore()

Restore deleted model.

<?php
// Since deleted models are ignored by default we have to show all models:
User::model()->withRemoved();
// or just turn off behavior completely
User::model()->disableBehavior('trash');

$user=User::model()->findByPk(1);

// if you turned behavior off, don't forget to turn it back on
User::model()->enableBehavior('trash');

$user->restore();
$user->save();

getIsRemoved()

Tells if model is marked as deleted.

<?php
$user1=User::model()->withRemoved()->findByPk(1);
echo $user1->getIsRemoved() ? 'status=removed' : 'status=normal';
$user2=User::model()->withRemoved()->findByPk(2);
echo $user2->isRemoved ? 'status=removed' : 'status=normal';

withRemoved()

Allows to search for deleted models.

<?php
$users=User::model()->withRemoved()->findAll();

About

AR models behavior that allows to mark models as deleted and then restore them.

Resources

Stars

Watchers

Forks

Packages

No packages published