Show Menu

RedBeanPHP Class Reference Cheat Sheet by richardjh

Easy ORM for PHP and MySQL, PostgreSQL and many other database systems. Use the simplicity of NoSQL with the power of SQL.

About RedBeanPHP ORM

Easy ORM for PHP and MySQL, PostgreSQL and many other database systems.
Use the simplicity of NoSQL with the power of SQL.

http:/­/re­dbe­anp­hp.com/

Class Reference pt1

addDat­abase ( $key, $dsn, $user=­null, $pass=­null, $froze­n=false )
Adds a database to the facade, afterwards you can select the database using select­Dat­aba­se(­$key).
addTags ( $bean, $tagList )
Part of RedBeanPHP Tagging API. Adds tags to a bean. If $tagList is a comma separated list of tags all tags will be associated with the bean. You may also pass an array instead of a string.
areRel­ated( $bean1, $bean2 )
Checks whether a pair of beans is related N-M. This function does not check whether the beans are related in N:1 way.
associate( $bean1, $bean2, $extra­=null )
Associates two Beans. This method will associate two beans with eachother. You can then get one of the beans by using the related() function and providing the other bean.
batch( $type, $ids )
Returns an array of beans. Pass a type and a series of ids and this method will bring you the corres­pondig beans.
begin()
Facade Convience method for adapter transa­ction system. Begins a transa­ction.
clearR­ela­tions( $bean, $type )
Clears all associated beans. Breaks all many-t­o-many associ­ations of a bean and a specified type.
close()
Closes the database connec­tion.
commit()
Facade Convience method for adapter transa­ction system. Commits a transa­ction.
config­ure­Fac­ade­Wit­hTo­olbox( $tb )
Configures the facade, want to have a new Writer? A new Object Database or a new Adapter and you want it on-the­-fly? Use this method to hot-swap your facade with a new toolbox.
conver­tTo­Beans( $type, $rows )
Converts a series of rows to beans.
count( $beanType )
Counts beans
debug( $tr=true )
Toggles DEBUG mode. In Debug mode all SQL that happens under the hood will be printed to the screen.
depend­encies( $dep )
Sets a list of depend­encies. A dependency list contains an entry for each dependent bean. A dependent bean will be removed if the relation with one of the depend­encies gets broken.
dispense( $type, $num )
Dispenses a new RedBean OODB Bean for use with the rest of the methods.
dispen­seL­abels( $type, $labels )
A label is a bean with only an id, type and name property. This function will dispense beans for all entries in the array. The values of the array will be assigned to the name property of each individual bean.
dup( $bean, $trail, $pid=false )
Makes a copy of a bean. This method makes a deep copy of the bean.
exec( $sql, $value­s=a­rray() )
Conven­ience function to execute Queries directly. Executes SQL.
exportAll( $beans )
Exports a collection of beans. Handy for XML/JSON exports with a Javascript framework like Dojo or ExtJS.
find( $type, $sql=null, $value­s=a­rray() )
Finds a bean using a type and a where clause (SQL). As with most Query tools in RedBean you can provide values to be inserted in the SQL statement by populating the value array parameter; you can either use the question mark notation or the slot-n­otation (:keyn­ame).
findAll( $type, $sql=null, $value­s=a­rray() )
Finds a bean using a type and a where clause (SQL). As with most Query tools in RedBean you can provide values to be inserted in the SQL statement by populating the value array parameter; you can either use the question mark notation or the slot-n­otation (:keyn­ame). The findAll() method differs from the find() method in that it does not assume a WHERE-­clause.
 

Class Reference pt2

findAn­dEx­port( $type, $sql=null, $value­=ar­ray() )
Finds a bean using a type and a where clause (SQL). As with most Query tools in RedBean you can provide values to be inserted in the SQL statement by populating the value array parameter; you can either use the question mark notation or the slot-n­otation (:keyn­ame). The variation also exports the beans (i.e. it returns arrays).
findLast( $type, $sql=null, $value­s=a­rray() )
Finds a bean using a type and a where clause (SQL). As with most Query tools in RedBean you can provide values to be inserted in the SQL statement by populating the value array parameter; you can either use the question mark notation or the slot-n­otation (:keyn­ame). This variation returns the last bean only.
findOne( $type, $sql=null, $value­s=a­rray() )
Finds a bean using a type and a where clause (SQL). As with most Query tools in RedBean you can provide values to be inserted in the SQL statement by populating the value array parameter; you can either use the question mark notation or the slot-n­otation (:keyn­ame). This variation returns the first bean only.
findOr­Dis­pense( $type, $sql, $values )
Conven­ience method. Tries to find beans of a certain type, if no beans are found, it dispenses a bean of that type.
freeze( $tr=true )
Toggles fluid or frozen mode. In fluid mode the database structure is adjusted to accomodate your objects. In frozen mode this is not the case.
gather­Labels( $beans )
Gathers labels from beans. This function loops through the beans, collects the values of the name properties of each individual bean and stores the names in a new array. The array then gets sorted using the default sort function of PHP (sort).
genSlots( $array )
Generates question mark slots for an array of values.
getAll( $sql, $value­s=a­rray() )
Conven­ience function to execute Queries directly. Executes SQL.
getAssoc( $sql, $value­s=a­rray() )
Conven­ience function to execute Queries directly. Executes SQL. Results will be returned as an associ­ative array. The first column in the select clause will be used for the keys in this array and the second column will be used for the values. If only one column is selected in the query, both key and value of the array will have the value of this field for each row.
getCell( $sql, $value­s=a­rray() )
Conven­ience function to execute Queries directly. Executes SQL.
getCol( $sql, $value­s=a­rray() )
Conven­ience function to execute Queries directly. Executes SQL.
getCol­umns( $table )
Returns a list of columns. Format of this array: array( fieldname => type ) Note that this method only works in fluid mode because it might be quite heavy on production servers!
getRow( $sql, $value­s=a­rray() )
Conven­ience function to execute Queries directly. Executes SQL.
getVer­sion()
Get version
graph( $array, $filte­rEm­pty­=false )
facade method for Cooker Graph.
hasTags( $bean, $tags, $all=false )
Part of RedBeanPHP Tagging API. Tests whether a bean has been associated with one ore more of the listed tags. If the third parameter is TRUE this method will return TRUE only if all tags that have been specified are indeed associated with the given bean, otherwise FALSE. If the third parameter is FALSE this method will return TRUE if one of the tags matches, FALSE if none match.
 

Class Reference pt3

isoDate( $time=null )
Simple conven­ience function, returns ISO date formatted repres­ent­ation of $time
isoDat­eTime( $time=null )
Simple conven­ience function, returns ISO date time formatted repres­ent­ation of $time.
load( $type, $id )
Loads the bean with the given type and id and returns it.
log( $filename )
Activates TimeLine Schema Alteration monitoring and Query logging.
nuke()
Nukes the entire database
query( $method, $sql, $values )
Internal Query function, executes the desired query. Used by all facade query functions. This keeps things DRY.
related( $bean, $type, $sql=null, $value­s=a­rray() )
Returns all the beans associated with $bean. This method will return an array containing all the beans that have been associated once with the associ­ate() function and are still associated with the bean specified.
relate­dOne( $bean, $type, $sql=null, $value­s=a­rray() )
Returns only single associated bean.
rollback()
Facade Convience method for adapter transa­ction system. Rolls back a transa­ction.
select­Dat­abase( $key )
Selects a different database for the Facade to work with.
setup( $dsn=null, $usern­ame­=null, $passw­ord­=null )
Kickstarts redbean for you. This method should be called before you start using RedBean. The Setup() method can be called without any arguments, in this case it will try to create a SQLite database in /tmp called red.db (this only works on UNIX-like systems).
store( $bean )
Stores a RedBean OODB Bean and returns the ID.
storeAll( $beans )
Short hand function to store a set of beans at once, IDs will be returned as an array. For inform­ation please consult the R::store() function. A loop saver.
swap( $beans, $property )
Given an array of two beans and a property, this method swaps the value of the property. This is handy if you need to swap the priority or orderNo of an item (i.e. bug-tr­acking, page order).
tag( $bean, $tagLi­st=null )
Part of RedBeanPHP Tagging API. Tags a bean or returns tags associated with a bean. If $tagList is null or omitted this method will return a comma separated list of tags associated with the bean provided. If $tagList is a comma separated list (string) of tags all tags will be associated with the bean. You may also pass an array instead of a string.
tagged( $beanType, $tagList )
Part of RedBeanPHP Tagging API. Returns all beans that have been tagged with one of the tags given.
trash( $bean )
Deletes the specified bean.
trashAll( $beans )
Short hand function to trash a set of beans at once. For inform­ation please consult the R::trash() function. A loop saver.
unasso­ciate( $bean1, $bean2, fast=false )
Breaks the associ­ation between two beans. This functions breaks the associ­ation between a pair of beans. After calling this functions the beans will no longer be associated with eachother. Calling related() with either one of the beans will no longer return the other bean.
unrelated( $bean, $type, $sql=null, $value­s=a­rray() )
The opposite of related(). Returns all the beans that are not associated with the bean provided.
untag( $bean, $tagList )
Part of RedBeanPHP Tagging API. Removes all sepcified tags from the bean. The tags specified in the second parameter will no longer be associated with the bean.
wipe( $beanType )
Wipes all beans of type $beanType.

Class Reference: See Also

Converted from the comments in the source code.
See http:/­/ww­w.r­edb­ean­php.co­m/a­pi/­d9/­daa­/cl­ass­_re­d_b­ean­___­fac­ade.html for more details.
 

Share This Cheat Sheet!

Favourited by 2 Members:

Theosis andshecodes2

Comments

Hamman Samuel Hamman Samuel, 13:54 11 Jul 12

Awesome post, thanks so much!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.