\RedBeanPHPAssociationManager

Association Manager.

The association manager can be used to create and manage many-to-many relations (for example sharedLists). In a many-to-many relation, one bean can be associated with many other beans, while each of those beans can also be related to multiple beans.

Summary

Methods
Properties
Constants
addEventListener()
signal()
__construct()
getTable()
associate()
relatedCount()
unassociate()
clearRelations()
related()
No public properties found
No constants found
associateBeans()
$oodb
$adapter
$writer
N/A
handleException()
relatedRows()
$observers
N/A

Properties

$observers

$observers : 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

__construct()

__construct(\RedBeanPHP\ToolBox  $tools) 

Constructor, creates a new instance of the Association Manager.

The association manager can be used to create and manage many-to-many relations (for example sharedLists). In a many-to-many relation, one bean can be associated with many other beans, while each of those beans can also be related to multiple beans. To create an Association Manager instance you'll need to pass a ToolBox object.

Parameters

\RedBeanPHP\ToolBox $tools

toolbox supplying core RedBeanPHP objects

getTable()

getTable(array  $types) : string

Creates a table name based on a types array.

Manages the get the correct name for the linking table for the types provided.

Parameters

array $types

2 types as strings

Returns

string

associate()

associate(\RedBeanPHP\OODBBean|array  $beans1, \RedBeanPHP\OODBBean|array  $beans2) : array

Associates two beans in a many-to-many relation.

This method will associate two beans and store the connection between the two in a link table. Instead of two single beans this method also accepts two sets of beans. Returns the ID or the IDs of the linking beans.

Parameters

\RedBeanPHP\OODBBean|array $beans1

one or more beans to form the association

\RedBeanPHP\OODBBean|array $beans2

one or more beans to form the association

Returns

array

relatedCount()

relatedCount(\RedBeanPHP\OODBBean|array  $bean, string  $type, string|NULL  $sql = NULL, array  $bindings = array()) : integer

Counts the number of related beans in an N-M relation.

This method returns the number of beans of type $type associated with reference bean(s) $bean. The query can be tuned using an SQL snippet for additional filtering.

Parameters

\RedBeanPHP\OODBBean|array $bean

a bean object or an array of beans

string $type

type of bean you're interested in

string|NULL $sql

SQL snippet (optional)

array $bindings

bindings for your SQL string

Returns

integer

unassociate()

unassociate(\RedBeanPHP\OODBBean  $beans1, \RedBeanPHP\OODBBean  $beans2, boolean  $fast = NULL) : void

Breaks the association between two beans. This method unassociates two beans. If the method succeeds the beans will no longer form an association. In the database this means that the association record will be removed. This method uses the OODB trash() method to remove the association links, thus giving FUSE models the opportunity to hook-in additional business logic. If the $fast parameter is set to boolean TRUE this method will remove the beans without their consent, bypassing FUSE. This can be used to improve performance.

Parameters

\RedBeanPHP\OODBBean $beans1

first bean in target association

\RedBeanPHP\OODBBean $beans2

second bean in target association

boolean $fast

if TRUE, removes the entries by query without FUSE

clearRelations()

clearRelations(\RedBeanPHP\OODBBean  $bean, string  $type) : void

Removes all relations for a bean. This method breaks every connection between a certain bean $bean and every other bean of type $type. Warning: this method is really fast because it uses a direct SQL query however it does not inform the models about this. If you want to notify FUSE models about deletion use a foreach-loop with unassociate() instead. (that might be slower though)

Parameters

\RedBeanPHP\OODBBean $bean

reference bean

string $type

type of beans that need to be unassociated

related()

related(\RedBeanPHP\OODBBean  $bean, string  $type, string  $sql = '', array  $bindings = array()) : array

Returns all the beans associated with $bean.

This method will return an array containing all the beans that have been associated once with the associate() function and are still associated with the bean specified. The type parameter indicates the type of beans you are looking for. You can also pass some extra SQL and values for that SQL to filter your results after fetching the related beans.

Don't try to make use of subqueries, a subquery using IN() seems to be slower than two queries!

Since 3.2, you can now also pass an array of beans instead just one bean as the first parameter.

Parameters

\RedBeanPHP\OODBBean $bean

the bean you have

string $type

the type of beans you want

string $sql

SQL snippet for extra filtering

array $bindings

values to be inserted in SQL slots

Returns

array

associateBeans()

associateBeans(\RedBeanPHP\OODBBean  $bean1, \RedBeanPHP\OODBBean  $bean2, \RedBeanPHP\OODBBean  $bean) : mixed

Associates a pair of beans. This method associates two beans, no matter what types. Accepts a base bean that contains data for the linking record.

This method is used by associate. This method also accepts a base bean to be used as the template for the link record in the database.

Parameters

\RedBeanPHP\OODBBean $bean1

first bean

\RedBeanPHP\OODBBean $bean2

second bean

\RedBeanPHP\OODBBean $bean

base bean (association record)

Returns

mixed

handleException()

handleException(\Exception  $exception) : void

Exception handler.

Fluid and Frozen mode have different ways of handling exceptions. Fluid mode (using the fluid repository) ignores exceptions caused by the following:

  • missing tables
  • missing column

In these situations, the repository will behave as if no beans could be found. This is because in fluid mode it might happen to query a table or column that has not been created yet. In frozen mode, this is not supposed to happen and the corresponding exceptions will be thrown.

Parameters

\Exception $exception

exception

relatedRows()

relatedRows(\RedBeanPHP\OODBBean  $bean, string  $type, string  $sql = '', array  $bindings = array()) : array

Internal method.

Returns the many-to-many related rows of table $type for bean $bean using additional SQL in $sql and $bindings bindings. If $getLinks is TRUE, link rows are returned instead.

Parameters

\RedBeanPHP\OODBBean $bean

reference bean instance

string $type

target bean type

string $sql

additional SQL snippet

array $bindings

bindings for query

Returns

array