GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing redbeanphp.com — RedBeanPHP :: Finding
FindingIf you do not know the ID of a bean, you can search for beans using the find method:
The find() method uses good old SQL.
No fancy, custom query language — just plain old SQL.
Find and SQLThe following example demonstrates how to use find() with bindings.
This find operation will return all beans of type 'book' having
a title that begins with the phrase: 'Learn to'.
There is no need to use mysql_real_escape.
Always use the bindings.
Hunting Beans (5.1+)To find and delete beans in one go:
As of RedBeanPHP 5.2: IN-queriesTo use a 'SELECT-IN' style query use the R::genSlots function to generate the correct number of '?' slots: Find OneIf you want a single bean instead of an array, use: If no beans match the criteria, this function will return NULL. As of 5.3 you can use explicit parameter binding if you like: As of 5.6.3 you can use pstr( $str ) to generate [ $str, PDO::PARAM_STR ] for you and pint( $num ) to generate [ $int, PDO::PARAM_INT ] - these are just shortcut functions for your convience. Find AllUse findAll if you don't want to add any conditions (but you want to order or limit... ) If no beans match your criteria, this function returns an empty array. Named slotsAll find methods: find, findOne and findAll also accept named slots: Besides querying beans, you can also use regular SQL queries. Cursors (4.2+)You can also use find with cursors: Or directly The advantage of using a cursor is that the entire collection will not be loaded into memory all at once. This is handy for dealing with large bean collections. Find like (4.2+)To find a bean matching certain criteria, you can use R::findLike(). The following code returns all flowers that are either yellow OR blue: Note that you can append some SQL here along with bindings. As of RedBeanPHP 5.2 you can also use beans as conditions: Find or create (4.2+)This works like R::findLike() but also creates (and stores) the bean if it does not exist yet... Find Multiple (4.2+)findMulti() takes a query and turns the result into several bean collections having different types: The first parameter of this function lists the types to load, the second parameter is the query, then come the optional query parameter bindings. The result of this operation will be something like: Besides loading various bean types at once from a query, this method can also restructure them, for instance to 'put the pages in the book' use (example of 4th parameter): The fourth parameter of findMulti takes an array containing arrays like the one above. The array in the example tells findMulti how to restructure the pages and the books. First it defines two variables 'a' and 'b' it then defines a matcher function, telling RedBeanPHP to execute the 'do' clause if the book_id of a page matches the id of a page. The 'do' clause then puts the page in the pageList of the selected book. While you can specify mappings like this, a better idea might be to write your own set of mapping functions returning structures like this. Finder::map helpers (5.3+)You can shorten the syntax above using the mapper helper: Use Finder::map to map the price data and the products to the shops. Finder-helpers like map() will return a mapping structure like described above, consisting of an array with a/b entries as well as a matcher function and a do-function. Use map() to map 1-N relations (one shop, many products), use onmap() to map N-1 relations (many users belong to one country) and use nmmap() for N-M relations (many books share many tags). Short Query Notation (SQN)If you wish to use a shorter syntax than SQL you can try my short query notation library. Example: As of RedBeanPHP 5.2 bean type names will be trimmed automatically. As of RedBeanPHP 5.2 the SQL parameter is optional. loadJoined() 5.5As of RedBeanPHP 5.5 you can use Finder::onmap for N-1 relations: But you can also use the loadJoined() shortcut:
Don't access parent beans in loops, that is bad for performance.
With 100 users the following code will run 101 queries: back to main menu
Donate to RedBeanPHP using Monero: RedBeanPHP Easy ORM for PHP © 2026 Gabor de Mooij
() and the RedBeanPHP community
(credits) - Licensed New BSD/GPLv2 -
RedBeanPHP Archives
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |