Import and Export
RedBeanPHP offers several functions to exchange data with beans.
You can import an array into a bean using:
$book->import( $_POST );
The code above is handy if your $_POST request array only contains book data. It will simply load all data into the book bean. You can also add a selection filter:
$book->import( $_POST, 'title,subtitle,summary,price' );
This will restrict the import to the fields specified. Note that this does not apply any form of validation to the bean. Validation rules have to be written in the model or the controller.
As of RedBeanPHP 5.7.2 you can use trimport() to automatically trim the values. The 2nd parameter of trimport() can be used to define a different function to apply. The rest of the parameters are the same as import().
$book->trimport( $_POST ); //trim values
$book->trimport( $_POST, 'strtoupper' ); //uppercase values
To import from another bean:
$book->importFrom( $otherBean );
Import using Dispense
Dispense can even convert a multi dimensional array to a bean hierarchy like this (use _type to indicate the type of the bean):
$book = R::dispense( [
'_type' => 'book',
'title' => 'Gifted Programmers',
'author' => [ '_type' => 'author', 'name' => 'Xavier' ],
'ownPageList' => [ ['_type'=>'page', 'text' => '...'] ]
R::dispense() also accepts multi dimensional arrays (4.2+)
To export the properties and values of a single bean use:
$array = $bean->export();
To recursively export one or an array of beans use:
$arrays = R::exportAll( $beans );
Bean lists in exports are keyless, 0 indexed. To also export parent beans:
$arrays = R::exportAll( $beans, TRUE );
Because exportAll() does not know about any aliases you have to use R::aliases( [ 'teacher' => 'person', ... ] ) to inform the export function about aliased beans.
back to main menu
Donate to RedBeanPHP using Monero:
(remove spaces and new lines)
Performance monitor: this page has been generated in 0.017538070678711s. Is the performance lacking? Please drop me an e-mail to notify me!