Symfony session get array value - arrays

I'm using Symfony2, and are having trouble getting array values stored in a session, without putting them in a variable or object.
Possible something like:
echo $app['session']->get('shop')->get('name');
Currently I'm achieving it by doing this, but I would like to avoid it for the cause of simplicity:
$temp = $app['session']->get('shop');
echo $temp['name'];
Is it possible?
Thanks in advance

The session object is just a "parameter bag", an object that holds keys & values.
If you want to create another level of that mechanism you would have to instantiate your own bag.
$shop = new \Symfony\Component\HttpFoundation\ParameterBag;
$shop->set('name', 'Fantastic Warehouse');
$app['session']->set('shop', $shop);
// next request
echo $app['session']->get('shop')->get('name');

Related

Rails update remove number from an array attribute?

Is there a way to remove a number from an attibute array in an update? For example, if I want to update all of an alchy's booze stashes if he runs out of a particular type of booze:
Alchy has_many :stashes
Stash.available_booze_types = [] (filled with booze.ids)
Booze is also a class
#booze.id = 7
if #booze.is_all_gone
#alchy.stashes.update(available_booze_types: "remove #booze.id")
end
update: #booze.id may or may not be present in the available_booze_types array
... so if #booze.id was in any of the Alchy.stash instances (in the available_booze_types attribute array), it would be removed.
I think you can do what you want in the following way:
if #booze.is_all_gone
#alchy.stashes.each do |stash|
stash.available_booze_types.delete(#booze.id)
end
end
However, it looks to me like there are better ways to do what you are trying to do. Rails gives you something like that array by using relations. Also, the data in the array will be lost if you reset the app (if as I understand available_booze_types is an attribute which is not stored in a database). If your application is correctly set up (an stash has many boozes), an scope like the following in Stash class seems to me like the correct approach:
scope :available_boozes, -> { joins(:boozes).where("number > ?", 0) }
You can use it in the following way:
#alchy.stashes.available_boozes
which would only return the ones that are available.

How to set multidimentional session array in Yii

I am trying to set a multidimensional session array using Yii but I keep getting the following error:
Indirect modification of overloaded element of CHttpSession has no effect
Can someone point me in the right direction, below is the code:
/ $params = Util::Get_POST_Params();
$session = Yii::app()->session;
$session['Cart']['OfferName'] = $params['offer_name'];
$session['Cart']['OfferFee'] = $params['offer_fee'];
Thank you.
After a little reading adding creating a temp array and using the Yii method "add" to add in the session, it did the trick form. below is the working code.
$session = Yii::app()->session;
$cartArray = array('OfferName'=>$params['offer_name'],'OfferFee'=>$params['offer_fee']);
$session->add('cart',$cartArray);

Creating new groups with an array

I am trying to create an array that will create new groups within the specific OU. If I populate the array with one site ex: $site=#('Dallas') it correctly creates the new groups, however if I place multiple sites in the array ex:$site=#('Dallas','Bedford') Powershell return an error "Can not resolve directory object for given identity 'OU=Dallas Bedford, DC=examplecompany,DC=COM trying to list "Dallas Bedford" as the site. I have tried with single quotations and double quotations.
Here is the foreach statement
foreach ($location in $site) {$pc="OU=$site,OU=Location,DC=examplecompany,DC=COM"
How can I fix this so I do not have to manually enter the site location one by one?
Thanks.
You're embedding the collection itself (e.g $site) instead of $location which holds the current array element, try this:
foreach ($location in $site) {
$pc="OU=$location,OU=Location,DC=examplecompany,DC=COM"
}

Mapping first 3 records in database to variables

I have a test spec where I use the following line of code to assign 3 variables to session tokens within my table:
#auth_token, #auth2_token, #auth3_token = Session.limit(3).map(&:token)
I now wish to assign 3 variables as a role classes from my Roles table which isn't restricted to one attribute only but the whole class. I have tried the following but it doesnt seem to be working:
#role1, #role2, #role3 = Role.limit(3).map
Can this be achieved? Any pointers would be greatly appreciated !!
It works for the auth tokens because map converts the relation object into an array which then gets assigned to the variables. For the roles just calling map returns an enumerable and not an array.
You can just call to_a directly on the relation object returned by the limit call in order to convert it to an array.
#role1, #role2, #role3 = Role.limit(3).to_a
Wasn't sure how to go about this but got round the problem using the following:
#role1 = Role.find_by_name!("First")
#role2 = Role.find_by_name!("Second")
#role3 = Role.find_by_name!("Third")

how to force drupal function to not use DB cache?

i have a module and i am using node_load(array('nid' => arg(1)));
now the problem is that this function keep getting its data for node_load from DB cache.
how can i force this function to not use DB cache?
Example
my link is http://mydomain.com/node/344983
now:
$node=node_load(array('nid'=>arg(1)),null,true);
echo $node->nid . " -- " arg(1);
output
435632 -- 435632
which is a randomly node id (available on the system)
and everytime i ctrl+F5 my browser i get new nid!!
Thanks for your help
Where are you calling this? For example, are you using it as part of your template.php file, as part of a page, or as an external module?
Unless you have this wrapped in a function with its own namespace, try naming the variable differently than $node -- for example, name it $my_node. Depending on the context, the 'node' name is very likely to be accessed and modified by Drupal core and other modules.
If this is happening inside of a function, try the following and let me know what the output is:
$test_node_1 = node_load(344983); // Any hard-coded $nid that actually exists
echo $test_node_1->nid;
$test_node_2 = node_load(arg(1)); // Consider using hook_menu loaders instead of arg() in the future, but that's another discussion
echo $test_node_2->nid;
$test_node_3 = menu_get_object(); // Another method that is better than arg()
echo $test_node_3->nid;
Edit:
Since you're using hook_block, I think I see your problem -- the block itself is being cached, not the node.
Try setting BLOCK_NO_CACHE or BLOCK_CACHE_PER_PAGE in hook_block, per the documentation at http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_block/6
You should also try to avoid arg() whenever possible -- it's a little bit of a security risk, and there are better ways to accomplish just about anything arg() would do in a module environment.
Edit:*
Some sample code that shows what I'm referring to:
function foo_block ($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0] = array(
'info' => 'I am a block!',
'status' => 1,
'cache' => BLOCK_NO_CACHE // Add this line
);
return $block;
case 'view':
.....
}
}
node_load uses db_query, which uses mysql_query -- so there's no way to easily change the database's cache through that function.
But, node_load does use Drupal's static $nodes cache -- It's possible that this is your problem instead of the database's cache. You can have node_load clear that cache by calling node_load with $reset = TRUE (node_load($nid, NULL, TRUE).
Full documentation is on the node_load manual page at http://api.drupal.org/api/drupal/modules--node--node.module/function/node_load/6
I have had luck passing in the node id to node_load not in an array.
node_load(1);
According to Druapl's api this is acceptable and it looks like if you pass in an array as the first variable it's loaded as an array of conditions to match against in the database query.
The issue is not with arg(), your issue is that you have caching enabled for anonymous users.
You can switch off caching, or you can exclude your module's menu items from the cache with the cache exclude module.
edit: As you've now explained that this is a block, you can use BLOCK_NO_CACHE in hook_block to exclude your block from the block cache.

Resources