Hi guys,
Am using RedBeans ORM in PHP,
Today only am going through the RedBeans ORM, i think RedBeans is the Very simple to manage database.
But am getting doubt at which is better to use either rows(queries) or beans.

`$sql = 'SELECT author.* FROM author 
        JOIN club WHERE club.id = 7 ';
    $rows = R::getAll($sql);
    $authors = R::convertToBeans('author',$rows);`



Plz explain me why we need to convert again to Beans..

Dani AI

Generated

Short answer: use arrays when you only need a lightweight, read-only result set; convert to beans when you want real RedBean behaviour (persistence, relations, meta, bean helpers). R::convertToBeans turns raw rows into OODBBean instances and supports a meta-mask for extra columns — that is the method you reach for when you want the result back as working RedBean objects. (redbeanphp.com)

When to prefer one over the other:

  • Arrays (R::getAll / raw SQL): best for reporting, exporting, or tight loops over very large result sets where minimal memory/cpu is important. ORMs add overhead — for heavy reads consider raw SQL or chunking. (stackoverflow.com)
  • Beans: use when you plan to modify and store results, traverse/modify relationships (ownX/sharedY), or rely on bean helpers and meta storage. Beans also let RedBean manage schema evolution when you persist changes. (sitepoint.com)

Practical patterns (avoid loading everything into memory):

  • Use findFromSQL when you want beans plus pagination or extra computed columns (it can return meta values alongside beans). Example pattern:
$books = R::findFromSQL(
  'book',
  'SELECT b.*, COUNT(p.id) AS extra_pages FROM book b LEFT JOIN page p ON p.book_id=b.id GROUP BY b.id',
  [],
  ['extra_pages']
);
$first = reset($books);
echo $first->info('extra_pages'); // meta value returned with the bean
  • To update/save a bean you loaded:
$a = R::load('author', $id);
$a->bio = 'updated text';
R::store($a);

For very large result sets, query in pages (LIMIT/OFFSET) or stream via PDO and convert/process each page. (redbeanphp.com)

Troubleshooting / gotchas: RedBean expects a proper primary key per table (use a bean formatter if your schema uses a different PK name). If convertToBeans seems wrong, check that your query returns the columns you expect (id included when appropriate) and consider using the metamask to keep computed columns out of the bean payload. (groups.google.com)

Notes: building on 's helpful point about raw arrays vs objects, use convertToBeans when you need the ORM features; otherwise stick with arrays for speed and simplicity. , — the RedBean docs and API pages linked above have concise examples for both approaches.

Recommended Answers

All 5 Replies

Member Avatar for Member #949455

Plz explain me why we need to convert again to Beans..

@Ramesh Konda

I don't understand your question?

What is the issue you are having?

It seem like you are testing out a query on a database.

`$sql = 'SELECT author.* FROM author
JOIN club WHERE club.id = 7 ';
$rows = R::getAll($sql);

here we are executing the query, and saving it in to $sql var.
and getting all values in to $rows right.
now to get the values we can use the $rows.
so again y we need to use

$authors = R::convertToBeans('author',$rows);` 

this(convertToBeans).

Member Avatar for Member #949455

so again y we need to use

@Ramesh Konda

What kind question is that?

You have to understand something, if you used any PHP framework, the framework itself has it own language and tags.

Did read this yet:

http://www.redbeanphp.com/welcome

The reason you ask this question because you don't understand how to used that language even though it's in PHP.

You need to post this question over there. The is reason is very simple.

I didn't write that code, RedBean did. They have a forum there.

Does that make sense?

thanks for u r replay... i/ll post it in redbean forum...

Just to answer to the question, the getAll() method will return a multidimensional array so, if you want to print the results, you have to loop it as an associative array:

foreach($rows as $author)
{
        echo $author['first_name'];
}

When you apply the convertToBeans() method you get an array of objects:

foreach($authors as $author)
{
        echo $author->first_name;
}

Here you can see the source of the file that defines the method, i.e. OODB.php:

In your example you can spot the difference by using print_r over the variables $rows and authors. Bye!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.