gabordemooij / redbean

ORM layer that creates models, config and database on the fly
https://www.redbeanphp.com
2.31k stars 279 forks source link

Save related data as meta #492

Closed flip111 closed 8 years ago

flip111 commented 8 years ago

Could convertToBeans allow additional columns to be put as meta data on the bean? This way you could do

SELECT library.*, COUNT(books.id) as book_count FROM library
JOIN books ON book.library_id = library_id

then in php

$beans = R::convertToBeans(R::getAll("the sql above"));

foreach ($beans as $bean) {
    $bean->normal_property;
    $bean->getMeta('book_count'); // aggregate
}
gabordemooij commented 8 years ago

This is difficult, how should RedBeanPHP determine which fields belong to the bean and which don't without storing a schema definition or parsing the query?

flip111 commented 8 years ago

storing a schema definition would be acceptable. another solution which i don't prefer is to let redbean know how many fields should be put in meta when making the query. (extra fields should be put last). It's it correct to say that redbean can only work with one type of bean per row? and not type1., type2. ??

gabordemooij commented 8 years ago

one of the selling points of RedBeanPHP is that it doesnt need to keep track of any schema.

gabordemooij commented 8 years ago

Maybe we could prefix fields in the query using aliasing 'meta_' ?

flip111 commented 8 years ago

Good idea :+1:

This is a bit OffTopic but, what about building an object graph from a query with a bunch of different tables? I'm sure that has come up along the way somewhere, what's your stance on that?

When redbean is not in freeze mode it keeps tracks of schema all the time actually. When it's frozen it could write out schema data to a php file and just include the file, it's very fast. Maybe some of these sorts of functionalities are better placed in plugins.

gabordemooij commented 8 years ago

meta mask has been implemented in master.

flip111 commented 8 years ago

what will happen if the name of the variable is the same as things that are already considered metadata?

gabordemooij commented 8 years ago

The meta data is namespaced, 'data.bundle' a collision is highly unlikely but it will simply overwrite the old data.

gabordemooij commented 8 years ago

offtopic: I see you like to play with parser, opcodes etc. Have you seen my other project, you might want to join.. https://github.com/gabordemooij/citrine

flip111 commented 8 years ago

That's quite the undertaking you got going on there, i saw it before it looks interesting. Perhaps you can showcase a few things on the website which were made with Citrine? Personally i'm trying to move away from imperative code into the functional land.

gabordemooij commented 8 years ago

Functional and OO are very close actually. Smalltalk was inspired by Lisp. Citrine brings the two back together as originally intended by Alan Kay. Object oriented programming is just 'functional programming without the need for types' - because your functions are bound to the types already.

gabordemooij commented 8 years ago

Besides Citrine is capable of mimicking functional paradigms, it supports closures and higher order functions - all without the need to change it's semantics, it's just a natural part of its design.