convertToBeans() public static method

This method converts a series of rows to beans. The type of the desired output beans can be specified in the first parameter. The second parameter is meant for the database result rows. Usage: $rows = R::getAll( 'SELECT * FROM ...' ) $beans = R::convertToBeans( $rows ); As of version 4.3.2 you can specify a meta-mask. Data from columns with names starting with the value specified in the mask will be transferred to the meta section of a bean (under data.bundle). $rows = R::getAll( 'SELECT FROM... COUNT(*) AS extra_count ...' ); $beans = R::convertToBeans( $rows ); $bean = reset( $beans ); $data = $bean->getMeta( 'data.bundle' ); $extra_count = $data['extra_count'];
public static convertToBeans ( string $type, array $rows, $metamask = NULL ) : array
$type string type of beans to produce
$rows array must contain an array of array
return array
Esempio n. 1
0
    /**
     * Tests whether we can send results of a query to meta data
     * when converting to bean.
     */
    public function testImportMeta()
    {
        R::nuke();
        $book = R::dispense(array('_type' => 'book', 'title' => 'Bean Recipes', 'author' => 'Meastro de la Bean'));
        $pages = R::dispenseAll('page*2');
        $book->ownPageList = reset($pages);
        R::store($book);
        $data = R::getRow('SELECT book.*,
								  COUNT(page.id) AS meta_count,
								  1234 AS meta_extra
						   FROM book
						   LEFT JOIN page ON page.book_id = book.id
						   GROUP BY book.id
						   ');
        $bean = R::convertToBean('book', $data, 'meta_');
        asrt(isset($bean->title), TRUE);
        asrt(isset($bean->author), TRUE);
        asrt(isset($bean->meta_count), FALSE);
        asrt(isset($bean->meta_extra), FALSE);
        $data = $bean->getMeta('data.bundle');
        asrt(intval($data['meta_count']), 2);
        asrt(intval($data['meta_extra']), 1234);
        //now with multiple beans
        $book = R::dispense(array('_type' => 'book', 'title' => 'Bean Adventures', 'author' => 'Mr Adventure'));
        $pages = R::dispenseAll('page*3');
        $book->ownPageList = reset($pages);
        R::store($book);
        $data = R::getAll('SELECT book.*,
								  COUNT(page.id) AS meta_pages
						   FROM book
						   LEFT JOIN page ON page.book_id = book.id
						   GROUP BY book.id
						   ');
        $books = R::convertToBeans('book', $data, 'meta_');
        $found = 0;
        foreach ($books as $book) {
            if ($book->title == 'Bean Recipes') {
                $found++;
                asrt(isset($book->title), TRUE);
                asrt(isset($book->author), TRUE);
                asrt(isset($book->meta_count), FALSE);
                asrt(isset($book->meta_extra), FALSE);
                $data = $book->getMeta('data.bundle');
                asrt(intval($data['meta_pages']), 2);
            }
            if ($book->title == 'Bean Adventures') {
                $found++;
                asrt(isset($book->title), TRUE);
                asrt(isset($book->author), TRUE);
                asrt(isset($book->meta_pages), FALSE);
                asrt(isset($book->meta_extra), FALSE);
                $data = $book->getMeta('data.bundle');
                asrt(intval($data['meta_pages']), 3);
            }
        }
        asrt($found, 2);
    }
Esempio n. 2
0
 public function testConv2Beans()
 {
     $row1 = array('id' => 1, 'title' => 'test');
     $row2 = array('id' => 2, 'title' => 'test2');
     $beans = R::convertToBeans('page', array($row1, $row2));
     asrt(count($beans), 2);
     asrt($beans[2]->title, 'test2');
 }