【问题标题】:cakePhp 3.4 AuthorizationcakePhp 3.4 授权
【发布时间】:2017-09-19 10:06:26
【问题描述】:

我正在尝试保存观看次数以进行计数。它仅在经过身份验证的用户访问此页面时保存记录。当未经身份验证的用户访问此功能时保存失败。

 use Cake\ORM\TableRegistry;
    use Cake\Event\Event;

      public function beforeFilter(Event $event)
            {
                    $this->Auth->allow(['view']);
            }
       public function view($photoId = null)
        {
            $photoViewsTable = TableRegistry::get('PhotoViews');
            $photoViews = $photoViewsTable->newEntity();
            $photoViews->ip_address = $_SERVER['REMOTE_ADDR'];
            $photoViews->photo_id = $photoId;
            $photoViews->user_id = ($this->request->session()->read('Auth.User.id')) ? $this->request->session()->read('Auth.User.id') : 0;
            $photoViews->ip_address = $_SERVER['REMOTE_ADDR'];
            $photoViewsTable->save($photoViews);
    }

【问题讨论】:

    标签: cakephp cakephp-3.4 role-base-authorization


    【解决方案1】:

    我认为这是因为您的模型 PhotoViews 中的验证规则。 检查您是否验证了 user_id 是否存在于表 users 中。

    将此行中的0改为null

    $photoViews->user_id = ($this->request->session()->read('Auth.User.id')) ? $this->request->session()->read('Auth.User.id') : null;
    

    一定要检查模型中的allowEmpty和数据库表中的null的验证。

    你可以通过得到这样的验证错误来调试它。

    if(!$photoViewsTable->save($photoViews)){
        debug($photoViewsTable->getErrors());
    }
    

    在登录和不登录的情况下检查此项。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多