- Getting started
- Basics
- Finding
- Relation Mapping
- Models
- Database
- BeanCan
- Advanced
- Custom getters
- Association API
- Copy Beans
- Tags
- Import and Export
- Debug
- Meta Data
- Architecture
- Other
Import and Export
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.
You can export the data inside a bean to an array like this:
$bookArray = $book->export();
Calling export() on a bean will export the data of a single bean into an array.
Recursive Export
To recursively export one or an array of beans use:
$arrays = R::exportAll( $beans );
Import functions do not validate user input.
You can use models to add validation on-the-fly.
Swap
It's very common in real life applications to swap properties. For instance, in a CMS you often have a feature to change the order of pages or menu items. To swap a property use:
$books = R::batch('book',array($id1,$id2));
R::swap($books,'rating');
We simply load two books using the batch loader, then we pass the array with two books to swap() as well as the name of the property we which to swap values of.
Tweet
User contributed notes. Please use the comment section to provide tips, notes and examples. To ask for support or to provide feedback use the forum. For bug reports use Github Issue Tracker.
Site comments powered by Disqus