\RedBeanPHPOODB

RedBean Object Oriented DataBase.

The RedBean OODB Class is the main class of RedBeanPHP. It takes OODBBean objects and stores them to and loads them from the database as well as providing other CRUD functions. This class acts as a object database.

Summary

Methods
Properties
Constants
addEventListener()
signal()
autoClearHistoryAfterStore()
__construct()
freeze()
isFrozen()
isChilled()
dispense()
setBeanHelper()
getBeanHelper()
check()
find()
findCollection()
tableExists()
store()
load()
trash()
batch()
convertToBeans()
count()
wipe()
getAssociationManager()
setAssociationManager()
getCurrentRepository()
bindFunc()
No public properties found
No constants found
unboxIfNeeded()
$chillList
$stash
$nesting
$writer
$isFrozen
$beanhelper
$assocManager
$repository
$frozenRepository
$fluidRepository
$autoClearHistoryAfterStore
N/A
No private methods found
$observers
$sqlFilters
N/A

Properties

$chillList

$chillList : array

Type

array

$stash

$stash : array

Type

array

$nesting

$nesting : 

Type

$isFrozen

$isFrozen : boolean

Type

boolean

$beanhelper

$beanhelper : \RedBeanPHP\FacadeBeanHelper

Type

\RedBeanPHP\FacadeBeanHelper

$autoClearHistoryAfterStore

$autoClearHistoryAfterStore : boolean

Type

boolean

$observers

$observers : array

Type

array

$sqlFilters

$sqlFilters : array

Type

array

Methods

addEventListener()

addEventListener(string  $eventname, \RedBeanPHP\Observer  $observer) : void

Implementation of the Observer Pattern.

Adds an event listener to the observable object. First argument should be the name of the event you wish to listen for. Second argument should be the object that wants to be notified in case the event occurs.

Parameters

string $eventname

event identifier

\RedBeanPHP\Observer $observer

observer instance

signal()

signal(string  $eventname, mixed  $info) : void

Notifies listeners.

Sends the signal $eventname, the event identifier and a message object to all observers that have been registered to receive notification for this event. Part of the observer pattern implementation in RedBeanPHP.

Parameters

string $eventname

event you want signal

mixed $info

message object to send along

autoClearHistoryAfterStore()

autoClearHistoryAfterStore(boolean  $autoClear = TRUE) : void

If set to TRUE, this method will call clearHistory every time the bean gets stored.

Parameters

boolean $autoClear

auto clear option

__construct()

__construct(\RedBeanPHP\QueryWriter  $writer, array|boolean  $frozen = FALSE) 

Constructor, requires a query writer.

Most of the time, you do not need to use this constructor, since the facade takes care of constructing and wiring the RedBeanPHP core objects. However if you would like to assemble an OODB instance yourself, this is how it works:

Usage:

$database = new RPDO( $dsn, $user, $pass ); $adapter = new DBAdapter( $database ); $writer = new PostgresWriter( $adapter ); $oodb = new OODB( $writer, FALSE ); $bean = $oodb->dispense( 'bean' ); $bean->name = 'coffeeBean'; $id = $oodb->store( $bean ); $bean = $oodb->load( 'bean', $id );

The example above creates the 3 RedBeanPHP core objects: the Adapter, the Query Writer and the OODB instance and wires them together. The example also demonstrates some of the methods that can be used with OODB, as you see, they closely resemble their facade counterparts.

The wiring process: create an RPDO instance using your database connection parameters. Create a database adapter from the RPDO object and pass that to the constructor of the writer. Next, create an OODB instance from the writer. Now you have an OODB object.

Parameters

\RedBeanPHP\QueryWriter $writer

writer

array|boolean $frozen

mode of operation: TRUE (frozen), FALSE (default, fluid) or ARRAY (chilled)

freeze()

freeze(boolean|array  $toggle) : void

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.

You can also pass an array containing a selection of frozen types. Let's call this chilly mode, it's just like fluid mode except that certain types (i.e. tables) aren't touched.

Parameters

boolean|array $toggle

TRUE if you want to use OODB instance in frozen mode

isFrozen()

isFrozen() : boolean

Returns the current mode of operation of RedBean.

In fluid mode the database structure is adjusted to accomodate your objects. In frozen mode this is not the case.

Returns

boolean

isChilled()

isChilled(string  $type) : boolean

Determines whether a type is in the chill list.

If a type is 'chilled' it's frozen, so its schema cannot be changed anymore. However other bean types may still be modified. This method is a convenience method for other objects to check if the schema of a certain type is locked for modification.

Parameters

string $type

the type you wish to check

Returns

boolean

dispense()

dispense(string  $type, string  $number = 1, boolean  $alwaysReturnArray = FALSE) : \RedBeanPHP\OODBBean

Dispenses a new bean (a OODBBean Bean Object) of the specified type. Always use this function to get an empty bean object. Never instantiate a OODBBean yourself because it needs to be configured before you can use it with RedBean. This function applies the appropriate initialization / configuration for you.

Parameters

string $type

type of bean you want to dispense

string $number

number of beans you would like to get

boolean $alwaysReturnArray

if TRUE always returns the result as an array

Returns

\RedBeanPHP\OODBBean

setBeanHelper()

setBeanHelper(\RedBeanPHP\BeanHelper  $beanhelper) : void

Sets bean helper to be given to beans.

Bean helpers assist beans in getting a reference to a toolbox.

Parameters

\RedBeanPHP\BeanHelper $beanhelper

helper

getBeanHelper()

getBeanHelper() : \RedBeanPHP\BeanHelper

Returns the current bean helper.

Bean helpers assist beans in getting a reference to a toolbox.

Returns

\RedBeanPHP\BeanHelper

check()

check(\RedBeanPHP\OODBBean  $bean) : void

Checks whether a OODBBean bean is valid.

If the type is not valid or the ID is not valid it will throw an exception: Security.

Parameters

\RedBeanPHP\OODBBean $bean

the bean that needs to be checked

find()

find(string  $type, array  $conditions = array(), string  $sql = NULL, array  $bindings = array()) : array

Searches the database for a bean that matches conditions $conditions and sql $addSQL and returns an array containing all the beans that have been found.

Conditions need to take form:

array( 'PROPERTY' => array( POSSIBLE VALUES... 'John', 'Steve' ) 'PROPERTY' => array( POSSIBLE VALUES... ) );

All conditions are glued together using the AND-operator, while all value lists are glued using IN-operators thus acting as OR-conditions.

Note that you can use property names; the columns will be extracted using the appropriate bean formatter.

Parameters

string $type

type of beans you are looking for

array $conditions

list of conditions

string $sql

SQL to be used in query

array $bindings

a list of values to bind to query parameters

Returns

array

findCollection()

findCollection(string  $type, string  $sql = NULL, array  $bindings = array()) : array

Same as find() but returns a BeanCollection.

Parameters

string $type

type of beans you are looking for

string $sql

SQL to be used in query

array $bindings

a list of values to bind to query parameters

Returns

array

tableExists()

tableExists(string  $table) : boolean

Checks whether the specified table already exists in the database.

Not part of the Object Database interface!

Parameters

string $table

table name

Returns

boolean

store()

store(\RedBeanPHP\OODBBean|\RedBeanPHP\SimpleModel  $bean) : integer|string

Stores a bean in the database. This method takes a OODBBean Bean Object $bean and stores it in the database. If the database schema is not compatible with this bean and RedBean runs in fluid mode the schema will be altered to store the bean correctly.

If the database schema is not compatible with this bean and RedBean runs in frozen mode it will throw an exception. This function returns the primary key ID of the inserted bean.

The return value is an integer if possible. If it is not possible to represent the value as an integer a string will be returned. We use explicit casts instead of functions to preserve performance (0.13 vs 0.28 for 10000 iterations on Core i3).

Parameters

\RedBeanPHP\OODBBean|\RedBeanPHP\SimpleModel $bean

bean to store

Returns

integer|string

load()

load(string  $type, integer  $id) : \RedBeanPHP\OODBBean

Loads a bean from the object database.

It searches for a OODBBean Bean Object in the database. It does not matter how this bean has been stored. RedBean uses the primary key ID $id and the string $type to find the bean. The $type specifies what kind of bean you are looking for; this is the same type as used with the dispense() function. If RedBean finds the bean it will return the OODB Bean object; if it cannot find the bean RedBean will return a new bean of type $type and with primary key ID 0. In the latter case it acts basically the same as dispense().

Important note: If the bean cannot be found in the database a new bean of the specified type will be generated and returned.

Parameters

string $type

type of bean you want to load

integer $id

ID of the bean you want to load

Returns

\RedBeanPHP\OODBBean

trash()

trash(\RedBeanPHP\OODBBean|\RedBeanPHP\SimpleModel  $bean) : void

Removes a bean from the database.

This function will remove the specified OODBBean Bean Object from the database.

Parameters

\RedBeanPHP\OODBBean|\RedBeanPHP\SimpleModel $bean

bean you want to remove from database

batch()

batch(string  $type, array  $ids) : array

Returns an array of beans. Pass a type and a series of ids and this method will bring you the corresponding beans.

important note: Because this method loads beans using the load() function (but faster) it will return empty beans with ID 0 for every bean that could not be located. The resulting beans will have the passed IDs as their keys.

Parameters

string $type

type of beans

array $ids

ids to load

Returns

array

convertToBeans()

convertToBeans(string  $type, array  $rows, string  $mask = NULL) : array

This is a convenience method; it converts database rows (arrays) into beans. Given a type and a set of rows this method will return an array of beans of the specified type loaded with the data fields provided by the result set from the database.

Parameters

string $type

type of beans you would like to have

array $rows

rows from the database result

string $mask

mask to apply for meta data

Returns

array

count()

count(string  $type, string  $addSQL = '', array  $bindings = array()) : integer

Counts the number of beans of type $type.

This method accepts a second argument to modify the count-query. A third argument can be used to provide bindings for the SQL snippet.

Parameters

string $type

type of bean we are looking for

string $addSQL

additional SQL snippet

array $bindings

parameters to bind to SQL

Returns

integer

wipe()

wipe(string  $type) : boolean

Trash all beans of a given type. Wipes an entire type of bean.

Parameters

string $type

type of bean you wish to delete all instances of

Returns

boolean

getAssociationManager()

getAssociationManager() : \RedBeanPHP\AssociationManager

Returns an Association Manager for use with OODB.

A simple getter function to obtain a reference to the association manager used for storage and more.

Returns

\RedBeanPHP\AssociationManager

setAssociationManager()

setAssociationManager(\RedBeanPHP\AssociationManager  $assocManager) : void

Sets the association manager instance to be used by this OODB.

A simple setter function to set the association manager to be used for storage and more.

Parameters

\RedBeanPHP\AssociationManager $assocManager

sets the association manager to be used

getCurrentRepository()

getCurrentRepository() : \RedBeanPHP\Repository

Returns the currently used repository instance.

For testing purposes only.

Returns

\RedBeanPHP\Repository

bindFunc()

bindFunc(string  $mode, string  $field, string  $function) : void

Binds an SQL function to a column.

This method can be used to setup a decode/encode scheme or perform UUID insertion. This method is especially useful for handling MySQL spatial columns, because they need to be processed first using the asText/GeomFromText functions.

Parameters

string $mode

mode to set function for, i.e. read or write

string $field

field (table.column) to bind SQL function to

string $function

SQL function to bind to field

unboxIfNeeded()

unboxIfNeeded(\RedBeanPHP\OODBBean  $bean) : \RedBeanPHP\OODBBean

Unboxes a bean from a FUSE model if needed and checks whether the bean is an instance of OODBBean.

Parameters

\RedBeanPHP\OODBBean $bean

bean you wish to unbox

Returns

\RedBeanPHP\OODBBean