The ORM model in laravel to modify the created_at, updated_at, deleted_at three

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The ORM model in the laravel framework greatly simplifies the operation of the database, but also improve the security of data operation.

There will be three time field, the default in the framework of laravel ORM model in created_at, updated_at, deleted_at, the three time field is the framework by default, no additional code to handle, convenient non reward:

Such as: UserModel:: create ($insert); ORM model will automatically add the create_at field but written in the current time, instead of our own to add this field, in the revision will be added updated_at,

In the deleted laravel framework provides two modes, one is deleted, a soft delete,

The normal delete deletes the database record directly, we also do not have to create the deleted_at field,

When using a soft delete, framework will not delete data in the database, but directly write deleted_at specifies the current delete time.

Note: when laravel used soft delete, in every normal query will add a deleted_at is null conditions, in order to remove deleted records, the current to remove deleted records to UserModel:: onlyTrashed()->get();


ORM facilitates the data operation in development, Only the default ORM model in the framework of laravel for the three time the data type of the field is of type datetime, But in the soft delete mode deleted_at must allow for a NULL, Even worse is if we modify the field type is int type deleted_at field must also allow for a NULL, Otherwise the soft delete will appear unable to access data, Because delete soft when using frame added by default condition is deleted_at is NULL for normal data, in some large amount of data in time field will use the int type, If the NULL is allowed, it will affect the index hit.

Specific modifications:


/**
* Specifies the time character
*
* @param \DateTime|int $value
* @return string
*/
public function fromDateTime($value)
{
return strtotime(parent::fromDateTime($value));
}

The code above is to modify the fields into the database data type is int


If the name this time we want to modify the three fields can add the following code to a corresponding MODEL class

const DELETED_AT='delete_at';
const UPDATED_AT='update_at';
const CREATED_AT = 'create_at';


The above processing only at work without the use of normal soft delete, if the use of soft delete must override the SoftDeletingTrait method in some, and extend the Illuminate\Database\Eloquent\SoftDeletingScope class

The following code:


trait SoftDelete{
use SoftDeletingTrait;
/**
* Create a soft delete object
*
* @return void
*/
public static function bootSoftDeletingTrait()
{
static::addGlobalScope(new SoftDeleteScope);
}
/**
* Only get soft deleted records
*
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public static function onlyTrashed()
{
$instance = new static;

$column = $instance->getQualifiedDeletedAtColumn();

return $instance->newQueryWithoutScope(new SoftDeleteScope)->where($column,'>',new Illuminate\Database\Query\Expression('0'));
}
/**
* Obtaining the soft delete records and normal.
*
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public static function withTrashed()
{
return with(new static)->newQueryWithoutScope(new SoftDeleteScope);
}
}

class SoftDeleteScope extends Illuminate\Database\Eloquent\SoftDeletingScope{
/**
* Only get the normal data
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
public function apply(Illuminate\Database\Eloquent\Builder $builder)
{
$model = $builder->getModel();

$builder->where($model->getQualifiedDeletedAtColumn(),'=',new Illuminate\Database\Query\Expression('0'));

$this->extend($builder);
}
/**
* Only get soft delete data
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addOnlyTrashed(Illuminate\Database\Eloquent\Builder $builder)
{
$builder->macro('onlyTrashed', function(Illuminate\Database\Eloquent\Builder $builder)
{
$this->remove($builder);

$builder->getQuery()->where($builder->getModel()->getQualifiedDeletedAtColumn(),'>',new Illuminate\Database\Query\Expression('0'));

return $builder;
});
}
/**
* Remove the soft delete conditions
*
* @param array $where
* @param string $column
* @return bool
*/
protected function isSoftDeleteConstraint(array $where, $column)
{
return $where['type'] == 'Basic' && $where['operator']=='=' && $where['value']=='0' && $where['column'] == $column;
}
}


Explain:

SoftDelete is part of the override the SoftDeletingTrait method, as long as we create the MODEL using use SoftDelete;

You can put a deleted_at field that completely changed to int type, and does not allow for a null.

The SoftDeleteScope class is to modify the add in soft delete deleted_at is null conditions.


Here, as long as we create MODEL inherits from Eloquent and add the use SoftDelete in the class; time field to override the fromDateTime method you can use the int type

Of course, also can be in the middle of Eloquent and Model to add a class to override the fromDateTime method, the time field can all implementations of model use the int type

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Philomena at August 16, 2014 - 10:14 AM