6

I'm using RedBean ORM to write some code and I was wondering if i can load/retrive only some fields from db table. I know there is a load method but it gives whole table as bean. I wish to get only some fields?

Heh, when i wrote it, I started wondering if it's not against RedBean pattern(or ORM), because getting only some values will create invalid(with only some values) object/bean? I wanted to make some lazy loading of values... maybe there is some other ORM(as easy as RedBean:) to achive this?

f1ames
  • 1,714
  • 1
  • 19
  • 36

1 Answers1

2

It does not make sense to load just some fields from a record:

  • By selecting less fields you won't decrease the number of queries
  • It wont decrease the amount of data that needs to be transferred (this is more related to the number of rows)

Also, RedBeanPHP already lazy-loads all relational fields, so there is no need to do this manual. If you are interested in only a single cell use:

R::getCell("select title from document where id = 1");

Or to just grab some fields from a record:

R::getRow("select id,title from document where... ");

These functions return records, not beans, this is the fastest way to deal with simple fields and rows.

Hopefully this answer helps...

Gabor de Mooij
  • 2,997
  • 17
  • 22
  • 5
    Sorry but I will have to disagree. Imagine a blog. It has 200 posts with a lot of text in the `body` property of each one. If you want to show a list of all posts you can easily run out of memory loading the `body` property of them all. But the thing is you don't need the `body` to show the list. Selective loading would save the day here. But anyway, I love your ORM and you did a really great job. Thank you so much :D – Hugo Mota Feb 28 '13 at 00:31