RedBeanPHP

easy ORM for PHP

Forms and Cooker

The cooker is a tool to turn arrays (forms, XML, JSON) into beans.


    
<form>
        <
input type="hidden" name="musician[type]" value="bandmember" />
        <
input type="text" name="musician[name]" value="" />
        <
input type="hidden" name="musician[ownInstrument][0][type]" value="instrument" />
        <
input type="text" name="musician[ownInstrument][0][name]" value="" /> 
    </
form>

Take a look at this little pseudo form. If you fill this form with band member name: 'Joe' and instrument name: 'Kazoo', you can turn this into a bean structure:


    $bandMember 
R::graph($_POST['musican']);

Your band member will now look like this:


    Bandmember Bean
        name
Joe
        ownInstrument

             
Instrument Bean
                 name
Kazoo

For an array to be converted to a bean it must resemble the structure of a bean. It must have a 'type' and properties. It can have nested arrays if they follow the the bean list conventions.

If an array has a field with key 'id', the Cooker will try to load the bean instead of dispensing a fresh bean. This means you can also update parts of beans.

R::graph($array,TRUE) will ignore all beans that appear to be empty (You can use this if you build forms; it makes it possible to add an empty form entry to add a new entity of something).

Demo App

Here is a little demo app that demonstrates the use of the Cooker. It's my Malt Management System, and I use it to keep track of my Single Malt Whisky collection, it's just one file of course.

Malt Management System

Be careful. Do NOT use the Cooker for publicly available forms. The Cooker does NOT check ID integrity. People could tamper with IDs or change fields they are not allowed to change. Only use the Cooker for trusted forms and feeds or with a thought-out ACL/rights system.

Learn how you can write Models that automatically connect to be beans using FUSE.



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

page generated in 0.010608911514282 sec.