dqdl6469 2012-08-10 07:07
浏览 48

在使用AJAX呼叫控制器时,在Symfony 1.4中将值写入/设置/更改为用户会话的替代方法?

I started this post to know how i can get/set Values from the User Session(Symfony 1.4). The Get works perfectly but the Set never works. I can see in the Debug Modus that the Global Session($_SESSION) is updated but the User Session dont. I tried this way and this way and another one and at least this... Without Success. I never read about such a Bug from Symfony 1.4. I use as Browser(linux) Firefox and Chrome (I tested it into anonymus Tab too).

Debug Modus User looks like :

attributeHolder:
myNS: { 
     aValue: '15', 
     ns_15: { 
         Tel: '78568951', 
         myID: '15', 
         anotherID: '120', 
         anArray: [{ 
             arrayID: '78', 
             Company: 'myComp'
         }] }, 
     ns_17: { ... } 
}

I can change the aValue attribute, delete and reinsert it. But the ns_15 or ns_17 can be delete or change but this Change cant be affect (symfony Debug Mode) User, but just into $_SESSION (symfony Debug Modus Globals ) i can see the updated values.

Is there another way to set/change/delete or write a value into an User Session with symfony 1.4 or it's a Symfony Bug?

EDIT1

I just dont figure it out til i see this Bug and this Blog !! well i'm using AJAX to call the Controller. So that's maybe the reason why i cant write on Session. Another Way i can't really subclass sfWebResponse cause my actions already subclass sfActions. :(

EDIT2

Because Edit1 I tried to make this:

  $sessionData = $this->getUser()->getAttribute("ns_15", array(), "myNS");//all good
  $sessionData["Tel"] = "0111111110";
  var_dump($sessionData["Tel"]);//show me 0111111110
  $this->getUser()->setAttribute("ns_15", $sessionData, "myNS");
  $this->getUser()->shutdown();
  sfContext::getInstance()->getStorage()->shutdown();

The Behavior with reloading twice is solved,but still cant write into $_SESSION and USER SESSION. (Same Trouble too).

  • 写回答

1条回答 默认 最新

  • duanaozhong0696 2012-08-10 10:39
    关注

    I try to summary how you can manipulate the user attributes (note that this not the same as modifying the $_SESSION global see my comment here to know why).

    // set a singe value in a namespace
    $this->getUser()->setAttribute('aValue', 15, 'myNS');
    // set an array in a namespace
    $this->getUser()->setAttribute('ns_15', array('Tel' => '78568951', 'myID' => '15', 'anotherID' => '120', 'anArray' => array(array('arrayID' => '78', 'Company' => 'myComp'))), 'myNS');
    
    // get the array from a namespace
    $attributes = $this->getUser()->getAttribute('ns_15', array(), 'myNS');
    // modify some value in the array
    $attributes['Tel'] = '12345';
    // remove some value from the array
    unset($attributes['myID']);
    // ... and save your changes back
    $this->getUser()->setAttribute('ns_15', $attributes, 'myNS');
    // remove the whole array from the namespase
    $this->getUser()->getAttributeHolder()->remove('ns_15', null, 'myNS');
    // remove the whole namespace
    $this->getUser()->getAttributeHolder()->removeNamespace('myNS');
    

    You can access a whole namespace as well:

    // get all attributes from a namespace
    $allAttributes = $this->getUser()->getAttributeHolder()->getAll('myNS');
    // manipulate the whole namespace here...
    // ...and save your changes back
    $this->getUser()->getAttributeHolder()->add($allAttributes, 'myNS');
    

    If you work on a same namespace and want to omit the namespace parameter:

    $originalDefaultNs = $this->getUser()->getAttributeHolder()->getDefaultNamespace();
    $this->getUser()->getAttributeHolder()->setDefaultNamespace('myNS', false);
    // ...
    $attributes = $this->getUser()->getAttribute('ns_15', array());
    // ...
    // don't forget to set back the original ns
    $this->getUser()->getAttributeHolder()->setDefaultNamespace($originalDefaultNS, false);
    

    I know it's really verbose, but I think there is no other way to do it in symfony. You can take a loot at the sfNamespacedParameterHolder class to see how these functions work.

    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch