【问题标题】:OctoberCMS update session value not workingOctoberCMS 更新会话值不起作用
【发布时间】:2020-09-10 05:57:13
【问题描述】:

我正在使用 OctoberCMS 会话 https://octobercms.com/docs/services/session,它对我来说工作正常,但我有一个要求。

我正在尝试通过首先找到一个会话数组 id 然后从该会话的数组中更新一个值来更新我的会话值..但这对我不起作用..这就是我正在做的事情..

我的 Html 文件

<a href="javascript:void(0)" data-request="{{ __SELF__ }}::onFramesSessionCreateOrUpdate" data-request-data="'productType':'1','productPlan':'2','productQty':'3'" data-request-update="cardlist: '#cardlist'">FrameS Add / Update Presenter</a>

我的 PHP 代码

public function onFramesSessionCreateOrUpdate(){
        $productType = post('productType');
        $productPlan = post('productPlan');
        $productQty = post('productQty');

        // First we are checking if this product plan is already exists or not 
        // If already exists, then we need to update the session, else we need to add as new session 

        if (\Session::has('addedToCart.frames'))
        {
            $sessionData = \Session::get('addedToCart.frames');
            if(!empty($sessionData))
            {
                foreach (\Session::get('addedToCart.frames') as $key => $value) 
                {
                    if ($value['productPlan'] === $productPlan)
                    {
                        \Session::push('addedToCart.frames.'.$key . '.productQty.', $productQty);
                        break;
                        
                    }
                }
            }
            else{
                $sessionId = \Str::random(9);
                $array = array(
                    'id' => $sessionId,
                    'productType'=>$productType,
                    'productPlan'  => $productPlan,
                    'productQty' => $productQty
                );
                \Session::push('addedToCart.frames' , $array);
                
            }
            
        }
        else{
            
            $sessionId = \Str::random(9);
            $array = array(
                'id' => $sessionId,
                'productType'=>$productType,
                'productPlan'  => $productPlan,
                'productQty' => $productQty
            );
            \Session::push('addedToCart.frames' , $array);
        }
        
       
    }

在我的 html 文件中,我在单击时调用函数 onFramesSessionCreateOrUpdate 并传递我的参数 productTypeproductPlanproductQty 和内部 onFramesSessionCreateOrUpdate 函数,首先我检查 addedToCart.frames 有一个 @ 987654330@.

如果它发现我只想更新其中的数量而不是删除会话..

\Session::push('addedToCart.frames.'.$key . '.productQty.', $productQty); 但是这段代码对我不起作用..

如果我关注print_r

echo '<pre>';
print_r($sessionData);
exit; 

我的数组是这样的

Array
(
    [0] => Array
        (
            [id] => SRgV5dJIC
            [productType] => 1
            [productPlan] => 2
            [productQty] => Array
                (
                    [] => Array
                        (
                            [0] => 3
                            [1] => 3
                        )

                )

        )

)

我也试过下面的代码

$productQty['productQty'] = $productQty;
\Session::push('addedToCart.frames.'.$key , $productQty);

然后错误提示legal string offset 'productQty'" on line 456 of D:\*\*\*\plugins\technobrave\loginplugin\components\LoginRegister.php

有人可以指导我如何更新会话.. 我不想使用Session::forget('key'); 删除此会话.. 我只想通过我找到的id 更新会话值。

【问题讨论】:

    标签: octobercms


    【解决方案1】:

    您再次尝试push 会话,所以it is trying to add value in array 而不是updating existing value

    你需要使用\Session::put()方法来override/replace value

    此代码将解决您的问题

    \Session::put('addedToCart.frames.' . $key . '.productQty', $productQty);
    

    如有任何疑问,请发表评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      相关资源
      最近更新 更多