Skip to content

Commit 58770a1

Browse files
committed
Harmonize convertToBean and getRow close #758.
1 parent cf1c4e6 commit 58770a1

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

RedBeanPHP/Facade.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1443,10 +1443,11 @@ public static function convertToBeans( $type, $rows, $metamask = NULL )
14431443
* @param array $row one row from the database
14441444
* @param string $metamask metamask (see convertToBeans)
14451445
*
1446-
* @return OODBBean
1446+
* @return OODBBean|NULL
14471447
*/
14481448
public static function convertToBean( $type, $row, $metamask = NULL )
14491449
{
1450+
if ( !count( $row ) ) return NULL;
14501451
$beans = self::$redbean->convertToBeans( $type, array( $row ), $metamask );
14511452
$bean = reset( $beans );
14521453
return $bean;

testing/RedUNIT/Base/Database.php

+18
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ public function getTargetDrivers()
4040
return array( 'mysql', 'pgsql', 'sqlite', 'CUBRID' );
4141
}
4242

43+
/**
44+
* Make ConvertToBean work together with getRow #759.
45+
* When no results are found for getRow it returns []
46+
* Then when you give that to convertToBean it wraps your
47+
* single row into an array of multiple rows, so you get [[]].
48+
* Then this loop has something to
49+
* iterate on foreach ( $rows as $row ) { ...
50+
* And then it crashes on: $id = $row['id'];
51+
*/
52+
public function testHarmonizeConvertToBeanAndGetRow()
53+
{
54+
R::nuke();
55+
$book = R::convertToBean( 'book', R::getRow( 'SELECT * FROM book' ) );
56+
asrt( is_null( $book ), TRUE );
57+
$book = R::convertToBean( 'book', array() );
58+
asrt( is_null( $book ), TRUE );
59+
}
60+
4361
/**
4462
* Test for bugfix:
4563
* adhere to return type specification for R::getRow #728

0 commit comments

Comments
 (0)