Jump to content
alejandro

Store array in session variable, then modify?

Recommended Posts

Hello, after creating an array and assigning it to a variable in session:

	$order = array ();
	$order['token'] 		= 'token';
	$order['product']		= 'product-name';
	$session->order 		= $order;

I try to insert another item:

$session->order['price'] = 'price';

But it doesn't work: 

Notice: Indirect modification of overloaded property ProcessWire\Session::$order has no effect in...

Isn't possible to modify such session variable? I could build another array from the session variable, add the new item, and then store it again in session, but doesn't looks good.

Thanks!

Share this post


Link to post
Share on other sites

As you said, get the array from session in a temp var, insert your new item then assign back the array to the session var :

$order = array();
$order['token'] 		= 'token';
$order['product']		= 'product-name';
$session->order 		= $order;
bd($session->order);
$tmp = $session->order;
$tmp['price'] = 'price';
$session->order = $tmp;
bd($session->order);

 

Or use $session->setFor() :

$session->setFor('shop', 'token', 'token');
$session->setFor('shop', 'product', 'product-name');
bd($session->shop);
$session->setFor('shop', 'price', 'price');
bd($session->shop);
$session->setFor('shop', 'price', 'new-price');
bd($session->shop);

 

Edited by flydev
solution #2
  • Like 4

Share this post


Link to post
Share on other sites

Thank you both.

I like solution #3 🙂

$session->order = array_merge ($session->order, ['price' => 50]);

didn't know about "setFor", didn't found in docs or cheatsheet. 

And, what's?

bd($session->shop);

The array_merge works without it

Share this post


Link to post
Share on other sites

😄 🙂

Well, I've just recently installed it, but I need to sit and learn to use it. It seems it's going to be of great help.

 

 

 

  • Like 1

Share this post


Link to post
Share on other sites
18 minutes ago, alejandro said:

And, what's?


bd($session->shop);

I want to add to tpr's comment that bd() is actually one of the 2 most used features of tracy for me. bd() makes a dump to the tracy bar (also for ajax requests, that's great!) and d() dumps directly and is great when using the console (see my screenshot above)

  • Like 2

Share this post


Link to post
Share on other sites
1 minute ago, bernhard said:

 bd() makes a dump to the tracy bar

already tested!

👍

Screen Shot 2018-06-22 at 11.21.29.png

  • Like 3

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By derelektrischemoench
      Hi guys,
      I'm facing a somewhat strange issue here which I can't quite wrap my head around. 
      I have a PW site in development which runs on three machines simultaneously, one staging server which is accessible as a preview instance for my customer, my PC and my laptop. 
      I have three completely identical settings on each of the three machines (same apache version, same php version, same codebase, same database); however on my PC I am unable to log into the backend. I get no error message or anything, when I try to login; i just get redirected to the login  page. I have already enabled database driven sessions (I enabled them on my laptop, then I dumped the database and copied it to my pc); I have cleared the cache directory; I cleared the sessions in the database; I cleared my browser caches, I tried different browsers, all to no avail; I am unable to login when using my pc, the instances all have the same .htaccess.
      Is there something I'm missing here or does anyone have a clue as to what my issue here might be? I'm using processwire 3.0.123
      Thanks for any input, greetings
      derelektrischemoench
       
      //edit: I've noticed something interesting; despite the directories of my web folders being the same layout; when I open the admin page i get a 404 on the processwire/ resource in the networks panel of chrome; on my laptop I get a  200.... I guess this is where my problem is; but why?
       
       
    • By derelektrischemoench
      Hi guys,
      I'm facing a somewhat strange issue here which I can't quite wrap my head around. 
      I have a PW site in development which runs on three machines simultaneously, one staging server which is accessible as a preview instance for my customer, my PC and my laptop. 
      I have three completely identical settings on each of the three machines (same apache version, same php version, same codebase, same database); however on my PC I am unable to log into the backend. I get no error message or anything, when I try to login; i just get redirected to the login  page. I have already enabled database driven sessions (I enabled them on my laptop, then I dumped the database and copied it to my pc); I have cleared the cache directory; I cleared the sessions in the database; I cleared my browser caches, I tried different browsers, all to no avail; I am unable to login when using my pc, the instances all have the same .htaccess.
      Is there something I'm missing here or does anyone have a clue as to what my issue here might be? I'm using processwire 3.0.123
      Thanks for any input, greetings
      derelektrischemoench
       
       
    • By Peter Knight
      How do you guys handle large session tables when sessions are being recorded to the database?
      I notice one of my sites has a session table of over 14MB 
      Am I missing a way in the Admin or a module to auto-remove any sessions older than X days?
      Thanks
       
    • By helmut2509
      In my PW-Application there is currently no session timeout.
      I want to set the user session to 60 minutes which means that after 60 minutes of inactivity the user will be redirected to the homepage.
      so I added the following entry to my config.php:
      $config->sessionExpireSeconds = 120; (120 seconds is just for testing).
      But after five minutes of inactivity I am still logged in, there is no redirection.
      Is there anything wrong or did I miss something?
      In php.ini I have the entry:
      session.cookie_lifetime = 3600
    • By celfred
      Hello,
      I'm facing a weird issue here. I have a page loaded with this code inside (my comments in line ends) :

      if ($session->allPlayers) { // Set in a head.inc file. I have also a $session->set('allTeams', $allTeams); in my head.inc   $allPlayers = $session->allPlayers; } else {   $allPlayers = getAllPlayers($user, false);   $session->set('allPlayers', $allPlayers); } bd($session->getAll()); // HERE, I get a number of 11 variables which is what I expect In the same page, I have a link pointing to ajaxContent.php that loads stuff via Ajax.
      I just write this in my ajaxContent.php to test :

      bd($session->getAll()); // HERE, I get only 9 variables. All my newly set $session variables ($allTeams and $allPlayers) are not conveyed to ajaxContent.php ??? Would you have any idea why is that ??? Another thing : I have a $session->headMenu set in my head.inc, and this one works fine. I can retrieve it in my ajaxContent.php page.
      I've tried cleaning all caches but it doesn't change anything 😞 
      At first, I expected it to be a 15-minute update to my site... It turns out to be a 2-hour issue and I'm still  stuck.
      Thanks for your ideas ! 
×
×
  • Create New...