How to save an array () in a RedBean property?

I got the following:

$post = (array) json_decode($post); $pushUser->dagen = (array) $post['days']; 

Part of the "days" of the message:

 [dagen] => Array ( [0] => Monday [1] => Wednesday ) 

All I want to do is store an array with days in $ pushUser-dagen :): ... pretty easy huh?

But then I get the following errors:

RedBean_Exception_Security exception with the message 'Invalid Bean: dagen property' in E: \ Documenten \ Dropbox \ Dropbox \ dummy-htdocs \ VID_service \ vid_push \ libs \ rb.php: 3465 Stack traces: # 0 E: \ Documenten \ Dropbox \ Dropbox \ dummy-htdocs \ VID_service \ vid_push \ libs \ rb.php (3496): RedBean_OODB-> check (Object (RedBean_OODBBean)) # 1 E: \ Documenten \ Dropbox \ Dropbox \ dummy-htdocs \ VID_service \ vid_push rb.php (7376): RedBean_OODB-> store (Object (RedBean_OODBBean)) # 2 E: \ Documenten \ Dropbox \ Dropbox \ dummy-htdocs \ VID_service \ vid_push \ api \ registerpush.php (32): R :: store ( object (RedBean_OODBBean)) # 3 {main}

Can't save arrays in a RedBean?

+4
source share
3 answers

It is not possible to store arrays in beans because RedBeanPHP cannot predict how you want the array to be represented in the database.

The decision depends on what you want to do with this information later. You want to add other things, and not just on weekdays, for example, in the evening or overtime (in case of planning?), In this case, I would choose the most flexible and easy solution:

 //Flexible solution foreach($days as $day) $user->sharedDay[] = R::findOne('day',' name = ? ',array($day)); 

Since we use sharedDay instead of ownDay, records will not be duplicated. This solution creates a neat day_user reference table.

Of course, you need to store weekly days in the database once:

  $days = array('monday',...); foreach($days as $dayname) { $d = R::dispense('day'); $d->name = $dayname; R::store($d); } 

If you want to be able to quickly track a user with a specific profile of the day of the week, you can encode days:

 //Performance solution function days($days) { $weekdays = array('sunday','monday','tuesday',...); $field = ''; foreach($weekdays as $day) { $field .= (in_array($day,$days)) ? '1' : '0'; } return $field; } $user->days = bindec(days($days)); 

In this case, you can find each user having "Monday" and "Saturday" by asking: days = 33. This is very fast.

If you do not need data at all, you can:

 //Quick and dirty solution $user->days = implode(',',$days); 
+3
source

http://redbeanphp.com/community/wiki/index.php/Tutorial#Loading_A_Bean

Check out import() . You need to format the arrays to store the key pair and values ​​as a column / row, etc.

 $bean->import($array); 

$bean->import($array, "key1,key4");

etc. You can always use array or regular expression search functions to find your value or format the array so that Redbean understands where Array is the key / value and the key must follow certain rules defined in the check () function in redbean, not forget about my head, just open rb.php and search for "checking public functions (" and you should find it ", but the last condition is that you must absolutely satisfy.

 $pattern = '/[^a-z0-9_]/i'; 

it should not be an array or an object, and the property should be a string> strlen from 2, otherwise it throws an invalid bean exception: property ...... It should be a flat array key = value, and it can be easily imported.

I hope this helps.

+3
source

One solution is to serialize the array using serialize ($ array) before storing it in a bean and deserializing ($ array) when retrieving it.

+1
source

Source: https://habr.com/ru/post/1387539/


All Articles