【问题标题】:Soft deleting with Sentry 2 and Laravel 4.2使用 Sentry 2 和 Laravel 4.2 进行软删除
【发布时间】:2015-05-12 13:31:14
【问题描述】:

我希望能够通过$user->delete(); 方法软删除用户,但它似乎无法正常工作。无论我做什么,它总是硬删除。我已经关注了所有关于它的 Laravel 4.2 文档,并且我认为我已经正确配置了 Sentry。这是我的代码:

用户.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' =&gt; 'User',

我还创建了迁移以在我的表上添加“deleted_at”列,所以我很茫然。任何帮助将不胜感激。

提前致谢!

【问题讨论】:

  • 嗨,Gabriel,您找到解决方案了吗?我面临着同样的问题。谢谢!
  • 我做到了!我会尽快发布答案:)

标签: php laravel soft-delete cartalyst-sentry


【解决方案1】:

所以,实际上解决方案非常简单。使用 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-&gt;delete(); 可以正常工作,添加一个“deleted_at”时间戳而不是销毁用户。

但这会带来一些问题。我还没有时间测试所有内容,但遇到了一些困难,例如 Sentry 模型无法查询已删除的结果,因此无法检索软删除的用户。现在我只是用User::withTrashed(); Laravel 方法来做,效果很好。

别忘了在/app/models/User.php下扩展Cartalyst的模型!

希望这也适用于其他人!

【讨论】:

  • 检查我在下面提供的答案,应该也适合你。 :)
【解决方案2】:

这个设置对我有用:

关于用户模型: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 文件夹中所做的更改,并且您的应用程序也不会出现错误。祝你好运!

【讨论】:

    猜你喜欢
    • 2014-10-19
    • 1970-01-01
    • 2013-07-29
    • 2015-07-13
    • 2015-04-28
    • 2021-07-07
    • 2014-09-28
    • 2014-11-06
    • 1970-01-01
    相关资源
    最近更新 更多