Skip to content

Commit

Permalink
added convenience method in facade findOrDispense()
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabor de Mooij, Buurtnerd committed Feb 13, 2011
1 parent 2ec9eb1 commit a098f01
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
15 changes: 15 additions & 0 deletions RedBean/Facade.php
Expand Up @@ -187,6 +187,21 @@ public static function loadOrDispense( $type, $id = 0 ) {
return ($id ? R::load($type,(int)$id) : R::dispense($type));
}

/**
* Convience method. Tries to find beans of a certain type,
* if no beans are found, it dispenses a bean of that type.
*
* @param string $type type of bean you are looking for
* @param string $sql SQL code for finding the bean
* @param array $values parameters to bind to SQL
*
* @return array $beans Contains RedBean_OODBBean instances
*/
public static function findOrDispense( $type, $sql, $values ) {
$foundBeans = self::find($type,$sql,$values);
if (count($foundBeans)==0) return self::dispense($type); else return $foundBeans;
}

/**
* Associates two Beans. This method will associate two beans with eachother.
* You can then get one of the beans by using the related() function and
Expand Down
1 change: 1 addition & 0 deletions RedBean/redbean.inc.php
Expand Up @@ -21,6 +21,7 @@
* files for you.
* @author Gabor de Mooij
* @license BSD
* @replica:exclude
*
* (c) G.J.G.T. (Gabor) de Mooij
* This source file is subject to the BSD/GPLv2 License that is bundled
Expand Down
9 changes: 8 additions & 1 deletion test.php
Expand Up @@ -127,7 +127,7 @@ public function onEvent($event, $info) {
$linker = new RedBean_LinkManager( $toolbox );

testpack("TEST VERSIONING");
asrt(R::getVersion(),"1.2.9.1");
asrt(R::getVersion(),"1.3beta");

//Section A: Config Testing
testpack("CONFIG TEST");
Expand Down Expand Up @@ -2591,6 +2591,13 @@ function getList($beans,$property) {
try{R::exec("select * from job limit ? ", array(1)); pass(); }catch(Exception $e){ fail(); }
try{R::exec("select * from job limit :l ", array(":l"=>1)); pass(); }catch(Exception $e){ fail(); }

testpack("Test findOrDispense");
$person = R::findOrDispense("person", " job = ? ", array("developer"));
asrt((count($person)>0), true);
$person = R::findOrDispense("person", " job = ? ", array("musician"));
asrt((count($person)>0), true);
$musician = array_pop($person);
asrt(intval($musician->id),0);

testpack("Test count and wipe");
$page = R::dispense("page");
Expand Down

0 comments on commit a098f01

Please sign in to comment.