Quote from: odeclas at Jan 03, 2011, 01:20 PM
Thanks Bob. As you can tell, I’m a relative novice at PHP - from reading around it seems it would be much simpler to create another array within the ’container’ array named after the key I want, and then simply use $array[]=$value to append the new $value at the end.
I’d got myself in a muddle by trying to reuse the code for the UpdateProfile snippet, which is aimed at doing a slightly different job.
What I still don’t know is whether it would be better to extend modUser instead, or even ignore both of these methods and use a custom table with remote-key in the user table as a foreign key. I guess if I had a clearer idea what I need the data for, rather than simply collecting the blessed stuff, that would help.
First, I definitely don’t claim to be a PHP wizard (though I do know some).
It’s always hard to make those decisions. I can’t really help because I don’t know what it is you’re saving, how many different items you’re saving over time, and what the possible values and speed considerations are.
You might want a custom table (although you definitely don’t want to be altering the user table in the process), but offhand, I would say probably not unless the data for each user will continue to grow and grow large over time.
Just a note about creating a container[] array. Remember that the user is an array containing the profile which is an array containing the extended array, which will then contain your container array. There’s nothing at all wrong with that, but it can make it hard to keep things straight when you’re programming, especially if you’re not used to nested arrays.
Here’s another way to do what you originally had (I think this is what you were describing):
<?php
$oldextended = $profile->get('extended');
$count = count($oldextended['container']) +1;
$oldextended[] = 'container' . $count] => $value;
$profile->set('extended', $oldextended);
$profile->save();
Another way to go if you’re just storing a list of IDs that have some relevance to the user is to make a comma-delimited string out of your data items, add new values on the end, and use exlode() when you want to get all values and strstr() (or explode() and in_array() ) when you want to test for one:
Adding a value would look like this:
<?
$oldextended = $profile->get('extended');
$newValue = 'someValue'
$oldextended['container'] = $oldextended['container'] . ',' . $newvalue;
$oldextended->save();
I like this method and it’s used often in MODx. It’s easier to debug, and it saves on unnecessary array overhead and indexing.