$toolbox
$toolbox : \RedBeanPHP\Util\Toolbox
Look Utility
The Look Utility class provides an easy way to generate tables and selects (pulldowns) from the database.
__construct(\RedBeanPHP\ToolBox $toolbox)
Constructor.
The MatchUp class requires a toolbox
\RedBeanPHP\ToolBox | $toolbox |
look(string $sql, array $bindings = array(), array $keys = array('selected', 'id', 'name'), string $template = '<option %s value="%s">%s</option>', callable $filter = 'trim', string $glue = '') : string
Takes an full SQL query with optional bindings, a series of keys, a template and optionally a filter function and glue and assembles a view from all this.
This is the fastest way from SQL to view. Typically this function is used to generate pulldown (select tag) menus with options queried from the database.
Usage:
$htmlPulldown = R::look(
'SELECT * FROM color WHERE value != ? ORDER BY value ASC',
[ 'g' ],
[ 'value', 'name' ],
'',
'strtoupper',
"\n"
);
The example above creates an HTML fragment like this:
to pick a color from a palette. The HTML fragment gets constructed by an SQL query that selects all colors that do not have value 'g' - this excludes green. Next, the bean properties 'value' and 'name' are mapped to the HTML template string, note that the order here is important. The mapping and the HTML template string follow vsprintf-rules. All property values are then passed through the specified filter function 'strtoupper' which in this case is a native PHP function to convert strings to uppercase characters only. Finally the resulting HTML fragment strings are glued together using a newline character specified in the last parameter for readability.
In previous versions of RedBeanPHP you had to use: R::getLook()->look() instead of R::look(). However to improve useability of the library the look() function can now directly be invoked from the facade.
string | $sql | query to execute |
array | $bindings | parameters to bind to slots mentioned in query or an empty array |
array | $keys | names in result collection to map to template |
string | $template | HTML template to fill with values associated with keys, use printf notation (i.e. %s) |
callable | $filter | function to pass values through (for translation for instance) |
string | $glue | optional glue to use when joining resulting strings |