PHP Class RedBeanPHP\AssociationManager

Manages simple bean associations.
Author: Gabor de Mooij and the RedBeanPHP Community
Inheritance: extends redbeanphp\Observable
Mostra file Open project: gabordemooij/redbean Class Usage Examples

Protected Properties

Property Type Description
$adapter RedBeanPHP\Adapter\DBAdapter
$oodb redbeanphp\OODB
$writer RedBeanPHP\QueryWriter

Public Methods

Method Description
__construct ( ToolBox $tools ) Constructor
associate ( redbeanphp\OODBBean | array $beans1, redbeanphp\OODBBean | array $beans2 ) : array Associates two beans in a many-to-many relation.
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)
getTable ( array $types ) : string Creates a table name based on a types array.
related ( redbeanphp\OODBBean | array $bean, string $type, string $sql = '', array $bindings = [] ) : array Returns all the beans associated with $bean.
relatedCount ( redbeanphp\OODBBean | array $bean, string $type, string $sql = NULL, array $bindings = [] ) : integer Counts the number of related beans in an N-M relation.
unassociate ( $beans1, $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.

Protected Methods

Method Description
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.

Private Methods

Method Description
handleException ( Exception $exception ) : void Handles exceptions. Suppresses exceptions caused by missing structures.
relatedRows ( redbeanphp\OODBBean $bean, string $type, string $sql = '', array $bindings = [] ) : array Internal method.

Method Details

__construct() public method

Constructor
public __construct ( ToolBox $tools )
$tools ToolBox toolbox

associate() public method

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.
public associate ( redbeanphp\OODBBean | array $beans1, redbeanphp\OODBBean | array $beans2 ) : array
$beans1 redbeanphp\OODBBean | array one or more beans to form the association
$beans2 redbeanphp\OODBBean | array one or more beans to form the association
return array

associateBeans() protected method

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.
protected associateBeans ( redbeanphp\OODBBean $bean1, redbeanphp\OODBBean $bean2, redbeanphp\OODBBean $bean ) : mixed
$bean1 redbeanphp\OODBBean first bean
$bean2 redbeanphp\OODBBean second bean
$bean redbeanphp\OODBBean base bean (association record)
return mixed

clearRelations() public method

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)
public clearRelations ( redbeanphp\OODBBean $bean, string $type ) : void
$bean redbeanphp\OODBBean reference bean
$type string type of beans that need to be unassociated
return void

getTable() public method

Manages the get the correct name for the linking table for the types provided.
public getTable ( array $types ) : string
$types array 2 types as strings
return string

relatedCount() public method

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.
public relatedCount ( redbeanphp\OODBBean | array $bean, string $type, string $sql = NULL, array $bindings = [] ) : integer
$bean redbeanphp\OODBBean | array a bean object or an array of beans
$type string type of bean you're interested in
$sql string SQL snippet (optional)
$bindings array bindings for your SQL string
return integer

unassociate() public method

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.
public unassociate ( $beans1, $beans2, boolean $fast = NULL ) : void
$fast boolean if TRUE, removes the entries by query without FUSE
return void

Property Details

$adapter protected_oe property

protected DBAdapter,RedBeanPHP\Adapter $adapter
return RedBeanPHP\Adapter\DBAdapter

$oodb protected_oe property

protected OODB,redbeanphp $oodb
return redbeanphp\OODB

$writer protected_oe property

protected QueryWriter,RedBeanPHP $writer
return RedBeanPHP\QueryWriter