\RedBeanPHPFinder

RedBeanPHP Finder.

Service class to find beans. For the most part this class offers user friendly utility methods for interacting with the OODB::find() method, which is rather complex. This class can be used to find beans using plain old SQL queries.

Summary

Methods
Properties
Constants
__construct()
map()
nmMap()
find()
findAndExport()
findOne()
findLast()
findOrDispense()
findCollection()
findOrCreate()
findLike()
findMulti()
No public properties found
No constants found
No protected methods found
$toolbox
$redbean
N/A
No private methods found
No private properties found
N/A

Properties

Methods

__construct()

__construct(\RedBeanPHP\ToolBox  $toolbox) 

Constructor.

The Finder requires a toolbox.

Parameters

\RedBeanPHP\ToolBox $toolbox

map()

map(string  $parentName, string  $childName) : array

A custom record-to-bean mapping function for findMulti.

Usage:

$collection = R::findMulti( 'shop,product,price', 'SELECT shop., product., price.* FROM shop LEFT JOIN product ON product.shop_id = shop.id LEFT JOIN price ON price.product_id = product.id', [], [ Finder::map( 'shop', 'product' ), Finder::map( 'product', 'price' ), ]);

Parameters

string $parentName

name of the parent bean

string $childName

name of the child bean

Returns

array

nmMap()

nmMap(string  $parentName, string  $childName) : array

A custom record-to-bean mapping function for findMulti.

Usage:

$collection = R::findMulti( 'book,book_tag,tag', 'SELECT book., book_tag., tag.* FROM book LEFT JOIN book_tag ON book_tag.book_id = book.id LEFT JOIN tag ON book_tag.tag_id = tag.id', [], [ Finder::nmMap( 'book', 'tag' ), ]);

Parameters

string $parentName

name of the parent bean

string $childName

name of the child bean

Returns

array

find()

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

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-notation (:keyname).

Parameters

string $type

type the type of bean you are looking for

string $sql

sql SQL query to find the desired bean, starting right after WHERE clause

array $bindings

values array of values to be bound to parameters in query

Returns

array

findAndExport()

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

Like find() but also exports the beans as an array.

This method will perform a find-operation. For every bean in the result collection this method will call the export() method. This method returns an array containing the array representations of every bean in the result set.

Parameters

string $type

type the type of bean you are looking for

string $sql

sql SQL query to find the desired bean, starting right after WHERE clause

array $bindings

values array of values to be bound to parameters in query

Returns

array

findOne()

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

Like find() but returns just one bean instead of an array of beans.

This method will return only the first bean of the array. If no beans are found, this method will return NULL.

Parameters

string $type

type the type of bean you are looking for

string $sql

sql SQL query to find the desired bean, starting right after WHERE clause

array $bindings

values array of values to be bound to parameters in query

Returns

\RedBeanPHP\OODBBean|NULL

findLast()

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

Like find() but returns the last bean of the result array.

Opposite of Finder::findLast(). If no beans are found, this method will return NULL.

Parameters

string $type

the type of bean you are looking for

string $sql

SQL query to find the desired bean, starting right after WHERE clause

array $bindings

values array of values to be bound to parameters in query

Returns

\RedBeanPHP\OODBBean|NULL

findOrDispense()

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

Tries to find beans of a certain type, if no beans are found, it dispenses a bean of that type.

Note that this function always returns an array.

Parameters

string $type

the type of bean you are looking for

string $sql

SQL query to find the desired bean, starting right after WHERE clause

array $bindings

values array of values to be bound to parameters in query

Returns

array

findCollection()

findCollection(string  $type, string  $sql, array  $bindings = array()) : \RedBeanPHP\BeanCollection

Finds a BeanCollection using the repository.

A bean collection can be used to retrieve one bean at a time using cursors - this is useful for processing large datasets. A bean collection will not load all beans into memory all at once, just one at a time.

Parameters

string $type

the type of bean you are looking for

string $sql

SQL query to find the desired bean, starting right after WHERE clause

array $bindings

values array of values to be bound to parameters in query

Returns

\RedBeanPHP\BeanCollection

findOrCreate()

findOrCreate(string  $type, array  $like = array(),   $sql = '') : \RedBeanPHP\OODBBean

Finds or creates a bean.

Tries to find a bean with certain properties specified in the second parameter ($like). If the bean is found, it will be returned. If multiple beans are found, only the first will be returned. If no beans match the criteria, a new bean will be dispensed, the criteria will be imported as properties and this new bean will be stored and returned.

Format of criteria set: property => value The criteria set also supports OR-conditions: property => array( value1, orValue2 )

Parameters

string $type

type of bean to search for

array $like

criteria set describing bean to search for

$sql

Returns

\RedBeanPHP\OODBBean

findLike()

findLike(string  $type, array  $conditions = array(), string  $sql = '', array  $bindings = array()) : array

Finds beans by its type and a certain criteria set.

Format of criteria set: property => value The criteria set also supports OR-conditions: property => array( value1, orValue2 )

If the additional SQL is a condition, this condition will be glued to the rest of the query using an AND operator. Note that this is as far as this method can go, there is no way to glue additional SQL using an OR-condition. This method provides access to an underlying mechanism in the RedBeanPHP architecture to find beans using criteria sets. However, please do not use this method for complex queries, use plain SQL instead ( the regular find method ) as it is more suitable for the job. This method is meant for basic search-by-example operations.

Parameters

string $type

type of bean to search for

array $conditions

criteria set describing the bean to search for

string $sql

additional SQL (for sorting)

array $bindings

bindings

Returns

array

findMulti()

findMulti(string|array  $types, string|array  $sql = NULL, array  $bindings = array(), array  $remappings = array(), string  $queryTemplate = ' %s.%s AS %s__%s') : array

Returns a hashmap with bean arrays keyed by type using an SQL query as its resource. Given an SQL query like 'SELECT movie.*, review.* FROM movie.

.. JOIN review' this method will return movie and review beans.

Example:

$stuff = $finder->findMulti('movie,review', ' SELECT movie., review. FROM movie LEFT JOIN review ON review.movie_id = movie.id');

After this operation, $stuff will contain an entry 'movie' containing all movies and an entry named 'review' containing all reviews (all beans). You can also pass bindings.

If you want to re-map your beans, so you can use $movie->ownReviewList without having RedBeanPHP executing an SQL query you can use the fourth parameter to define a selection of remapping closures.

The remapping argument (optional) should contain an array of arrays. Each array in the remapping array should contain the following entries:

array( 'a' => TYPE A 'b' => TYPE B 'matcher' => MATCHING FUNCTION ACCEPTING A, B and ALL BEANS 'do' => OPERATION FUNCTION ACCEPTING A, B, ALL BEANS, ALL REMAPPINGS )

Using this mechanism you can build your own 'preloader' with tiny function snippets (and those can be re-used and shared online of course).

Example:

array( 'a' => 'movie' //define A as movie 'b' => 'review' //define B as review 'matcher' => function( $a, $b ) { return ( $b->movie_id == $a->id ); //Perform action if review.movie_id equals movie.id } 'do' => function( $a, $b ) { $a->noLoad()->ownReviewList[] = $b; //Add the review to the movie $a->clearHistory(); //optional, act 'as if these beans have been loaded through ownReviewList'. } )

The Query Template parameter is optional as well but can be used to set a different SQL template (sprintf-style) for processing the original query.

Parameters

string|array $types

a list of types (either array or comma separated string)

string|array $sql

optional, an SQL query or an array of prefetched records

array $bindings

optional, bindings for SQL query

array $remappings

optional, an array of remapping arrays

string $queryTemplate

optional, query template

Returns

array