custom_fields array - access elements from PHP

eriche

Member
Hello,

I am extending the Datawriter - User.php
Works good so far.

My Problem only is that I don`t know how to Access Array fields in PHP o_O

PHP:
  ["_POST"] => array(48) {
    ["username"] => string(7) "tester1"

I can Access with
PHP:
$this->get('username')


BUT how to Access a custom userfield (Array of Array) :(
PHP:
  ["_POST"] => array(48) {
    ["custom_fields"] => array(8) {
      ["facebook"] => string(0) "1234567890"

how to get the "Facebook" Content?

thanks very much!

Erich
 
Looks like you are trying to access data from an HTTP request via a DataWriter (two different things). The DataWriter lets you access data from your database... and MySQL has no array column types, so it would be impossible for you use the DataWriter get() method to access an impossible type of database data.

If you want to store an array in a MySQL table (without getting into the complexities of creating a new table to join to), you can just serialize() or json_encode() the array (which will result in a string you can store in your database). And then you unserialize() or json_decode() the string when you get it out of the database.
 
I am so glad for your Response - many development questions are never answered here, I lost the believe already,... :cry:

In the time I found a way to get the Information I Need.
PHP:
$fields = unserialize($this->get('custom_fields'));

with this way I get the $fields Array and can access all the custom fields stored as blob in the DB.

But I thought there will be a better way o_O

Because right now when I wanna check if something has changed I have to do it over all custom fields
PHP:
$this->isChanged('custom_fields')

Maybe someone knows a way how to check if just the Facebook field has changed :rolleyes:

And at the beginning I thought that when extending the User.php there might be a way to access the e.g. the facebook field without unserializing the whole blob string.
Just via a 2nd dimension in an array like get('custom_fields')['facebook'],...

The Post data was a forced crash - that I see what variables are available :cool:
And from the Post data is looks for me that Facebook is just a dimension of "custom_fields" and you could access it directly.

So if somebody knows how to make this in a perfect way - tell me.
My solution is working / but I always like to learn from beginning how to do it the right way ;)

Erich
 
That's the best way to do it (at least within the DataWriter)... the DataWriter is basing stuff on the database structure... So it's probably not best to use the isChanged() method for checking if serialized data changed when you really just care about 1 value within it. If you must do it in the DataWriter, your best option is to unserialize both the new and old values and just do a normal == compare to see if that one value changed.
 
@digitalpoint as nooby I just was guessing that the DataWriter may be the best place for me :unsure:
I just want, if a user changes something in his "personal details" or "contact details", that I can trigger a stored procedure with this parameters getting changed.
For my external Platform I wanna bring closer to the Xenforo I need this.

So if there is a better way to hook another PHP, please tell me.

I don`t understand how I can get the old values :rolleyes:
When the "presave" function is triggered, I think I only have the new values.
So maybe you can guide me here.

1000 thanks!

Erich
 
The DataWriter has methods for getting new data and getting old data specifically.

For example:
PHP:
$this->getNew('username');
$this->getExisting('username');

That should allow you to then unserialize the custom fields for a normal comparison on just what you want to compare.
 
Top Bottom