使用 Sentry 2 和 Laravel 4.2 进行软删除
Soft deleting with Sentry 2 and Laravel 4.2
我希望能够通过$user->delete();
方法软删除用户,但它似乎无法正常工作。无论我做什么,它总是很难删除。我已经遵循了所有关于它的 Laravel 4.2 文档,并且我认为我已经正确配置了 Sentry。这是我的代码:
User.php
<?php
use Cartalyst\Sentry\Sentry;
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class User extends Cartalyst\Sentry\Users\Eloquent\User implements UserInterface, RemindableInterface
{
protected $fillabe = array('username','email','password','password_temp','code','active');
use UserTrait, RemindableTrait, SoftDeletingTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
//Soft deleting
protected $dates = ['deleted_at'];
public $timestamps = true;
}
Controller.php
public function postDeactivate()
{
$validator = Validator::make(Input::all(), array('password' => 'required|min:6'));
if ($validator->fails()) {
return Redirect::route('account-deactivate')->withErrors($validator);
} else {
try {
$user = Sentry::getUser();
if ($user->checkPassword(Input::get('password'))) {
Mail::send(
'emails.deactivate',
array('username' => $user->username),
function($message) use ($user) {
$message->to($user->email, $user->username)->subject('Hope to see you soon!');
}
);
if ($user->delete()) {
return Redirect::route('home') ->with('global','Account deactivated!');
} else {
return Redirect::route('account-deactivate')->with('global','Error!');
}
} else {
return Redirect::route('account-deactivate')->with('global','Wrong password.');
}
} catch (Cartalyst\Sentry\Users\UserNotFoundException $e) {
return Redirect::route('account-sign-in-get')->with('message','User not found!');
}
}
顺便说一下,我已经在
下发布了哨兵配置文件
/vendor/cartalyst/sentry/src/config/config.php
并将第 123 行更改为
'model' => 'User',
我还创建了迁移以在我的 table 上添加 'deleted_at' 列,所以我不知所措。任何帮助将不胜感激。
提前致谢!
所以,实际上解决方案非常简单。使用 Sentry 时,您使用的不是 Laravel 预配置的 Eloquent 模型,而是 Cartalyst 自己的模型。所以我需要做的是在 /vendor/cartalyst/sentry/src/Cartalyst/Sentry/Users/Eloquent/User.php
下的 Sentry 用户模型中获取软删除声明,而不是我在打开此问题时尝试的声明。
这是我的哨兵用户模型:
<?php namespace Cartalyst\Sentry\Users\Eloquent;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Illuminate\Database\Eloquent\Model;
use Cartalyst\Sentry\Groups\GroupInterface;
use Cartalyst\Sentry\Hashing\HasherInterface;
use Cartalyst\Sentry\Users\LoginRequiredException;
use Cartalyst\Sentry\Users\PasswordRequiredException;
use Cartalyst\Sentry\Users\UserAlreadyActivatedException;
use Cartalyst\Sentry\Users\UserExistsException;
use Cartalyst\Sentry\Users\UserInterface;
class User extends Model implements UserInterface {
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'users';
现在 $user->delete();
可以正常工作,添加 'deleted_at' 时间戳而不是销毁用户。
虽然这会带来一些问题。我还没有时间测试所有内容,但我遇到了一些困难,例如哨兵模型无法使用垃圾结果进行查询,因此无法检索软删除的用户。现在我只是用 User::withTrashed();
Laravel 方法来做,效果很好。
并且不要忘记在 /app/models/User.php
!
下扩展 Cartalyst 的模型
希望这也适用于其他人!
这个设置对我有用:
在用户模型上:app/models/User.php
<?php
use Cartalyst\Sentry\Users\Eloquent\User as SentryUserModel;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class User extends SentryUserModel {
use SoftDeletingTrait;
protected $softDelete = true;
protected $dates = ['deleted_at'];
在我的用户迁移中添加了 deleted_at 字段:
class UpdateUsersTable extends Migration {
public function up()
{
Schema::table('users', function(Blueprint $table)
$table->datetime('deleted_at');
});
}
public function down()
{
Schema::table('users', function(Blueprint $table)
{
$table->dropColumn('deleted_at');
});
}
发布哨兵配置文件
php artisan config:publish --path="vendor/cartalyst/sentry/src/config/config.php" cartalyst/sentry
我更改了该已发布文件的第 123 行以使用 laravel 用户模型:(app/config/packages/cartalyst/sentry/config.php)
// 'model' => 'Cartalyst\Sentry\Users\Eloquent\User',
'model' => 'User',
有效,甚至这个有效:User::withTrashed()->get();
如果这不起作用,请尝试重新清除数据库 运行 迁移。
这种方法的好处是,即使在更新 sentry 之后,您也不会丢失 vendor 文件夹中的更改,并且您的应用程序不会有错误。祝你好运!
我希望能够通过$user->delete();
方法软删除用户,但它似乎无法正常工作。无论我做什么,它总是很难删除。我已经遵循了所有关于它的 Laravel 4.2 文档,并且我认为我已经正确配置了 Sentry。这是我的代码:
User.php
<?php
use Cartalyst\Sentry\Sentry;
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class User extends Cartalyst\Sentry\Users\Eloquent\User implements UserInterface, RemindableInterface
{
protected $fillabe = array('username','email','password','password_temp','code','active');
use UserTrait, RemindableTrait, SoftDeletingTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
//Soft deleting
protected $dates = ['deleted_at'];
public $timestamps = true;
}
Controller.php
public function postDeactivate()
{
$validator = Validator::make(Input::all(), array('password' => 'required|min:6'));
if ($validator->fails()) {
return Redirect::route('account-deactivate')->withErrors($validator);
} else {
try {
$user = Sentry::getUser();
if ($user->checkPassword(Input::get('password'))) {
Mail::send(
'emails.deactivate',
array('username' => $user->username),
function($message) use ($user) {
$message->to($user->email, $user->username)->subject('Hope to see you soon!');
}
);
if ($user->delete()) {
return Redirect::route('home') ->with('global','Account deactivated!');
} else {
return Redirect::route('account-deactivate')->with('global','Error!');
}
} else {
return Redirect::route('account-deactivate')->with('global','Wrong password.');
}
} catch (Cartalyst\Sentry\Users\UserNotFoundException $e) {
return Redirect::route('account-sign-in-get')->with('message','User not found!');
}
}
顺便说一下,我已经在
下发布了哨兵配置文件/vendor/cartalyst/sentry/src/config/config.php
并将第 123 行更改为
'model' => 'User',
我还创建了迁移以在我的 table 上添加 'deleted_at' 列,所以我不知所措。任何帮助将不胜感激。
提前致谢!
所以,实际上解决方案非常简单。使用 Sentry 时,您使用的不是 Laravel 预配置的 Eloquent 模型,而是 Cartalyst 自己的模型。所以我需要做的是在 /vendor/cartalyst/sentry/src/Cartalyst/Sentry/Users/Eloquent/User.php
下的 Sentry 用户模型中获取软删除声明,而不是我在打开此问题时尝试的声明。
这是我的哨兵用户模型:
<?php namespace Cartalyst\Sentry\Users\Eloquent;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Illuminate\Database\Eloquent\Model;
use Cartalyst\Sentry\Groups\GroupInterface;
use Cartalyst\Sentry\Hashing\HasherInterface;
use Cartalyst\Sentry\Users\LoginRequiredException;
use Cartalyst\Sentry\Users\PasswordRequiredException;
use Cartalyst\Sentry\Users\UserAlreadyActivatedException;
use Cartalyst\Sentry\Users\UserExistsException;
use Cartalyst\Sentry\Users\UserInterface;
class User extends Model implements UserInterface {
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'users';
现在 $user->delete();
可以正常工作,添加 'deleted_at' 时间戳而不是销毁用户。
虽然这会带来一些问题。我还没有时间测试所有内容,但我遇到了一些困难,例如哨兵模型无法使用垃圾结果进行查询,因此无法检索软删除的用户。现在我只是用 User::withTrashed();
Laravel 方法来做,效果很好。
并且不要忘记在 /app/models/User.php
!
希望这也适用于其他人!
这个设置对我有用:
在用户模型上:app/models/User.php
<?php
use Cartalyst\Sentry\Users\Eloquent\User as SentryUserModel;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class User extends SentryUserModel {
use SoftDeletingTrait;
protected $softDelete = true;
protected $dates = ['deleted_at'];
在我的用户迁移中添加了 deleted_at 字段:
class UpdateUsersTable extends Migration {
public function up()
{
Schema::table('users', function(Blueprint $table)
$table->datetime('deleted_at');
});
}
public function down()
{
Schema::table('users', function(Blueprint $table)
{
$table->dropColumn('deleted_at');
});
}
发布哨兵配置文件
php artisan config:publish --path="vendor/cartalyst/sentry/src/config/config.php" cartalyst/sentry
我更改了该已发布文件的第 123 行以使用 laravel 用户模型:(app/config/packages/cartalyst/sentry/config.php)
// 'model' => 'Cartalyst\Sentry\Users\Eloquent\User',
'model' => 'User',
有效,甚至这个有效:User::withTrashed()->get();
如果这不起作用,请尝试重新清除数据库 运行 迁移。
这种方法的好处是,即使在更新 sentry 之后,您也不会丢失 vendor 文件夹中的更改,并且您的应用程序不会有错误。祝你好运!