RedBeanPHP

easy ORM for PHP

Finding Beans

RedBeanPHP allows you to use good old SQL to find beans:


    $needles 
R::find('needle',' haystack = ?', array( $haystack ));

Find takes 3 arguments; the type of bean you are looking for, the SQL query and an array containing the values to be used for the question marks. You can also use named slots in your queries:


    $needles 
R::find('needle',' haystack = :haystack 
                                  ORDER BY :sortorder'

    array( 
':sortorder'=>$sortorder':haystack'=>$haystack ));

To find all needles:


    $all 
R::find('needle');

To find all needles without conditions but with ORDER or LIMIT:


    $all 
R::find('needle',' 1 ORDER BY title LIMIT 2 ');

The SQL snippet starts with 1, this is because the find() functions expect a WHERE condition. Instead of an array, find() can return just the first record:


    $all 
R::findOne('needle',' 1 ORDER BY title LIMIT 1 ');

You can use IN-clauses as well:


    $all 
R::find('needle',' id IN ('.R::genSlots($ids).') ',$ids);

If no beans are found, find() returns an empty array and findOne() returns NULL.

From RedBeanPHP 3.2 on you no longer have to use the '1' prefix to query without a WHERE clause you can simply use R::findAll().

the 'WHERE' keyword is not necessary in find()



User contributed notes. Please use the comment section to provide tips, notes and examples. To ask for support or to provide feedback use the forum. For bug reports use Github Issue Tracker.

Site comments powered by Disqus

page generated in 0.010657072067261 sec.