Re: OO -> RDBMS mapping
| From: | Nezar Nielsen | Date: | Fri, 01 Nov 2002 15:01:09 +0000 |
| Subject: | Re: OO -> RDBMS mapping | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-10422@lists.php.net to get a copy of this message | ||
Andi Gutmans wrote:
> Has there been any talk on developing something similar to JDO (Java Data
> Objects) in PEAR?
Hi,
I don't know anything about JDO, but I'm currently working on a
OO-persistance system, that I had planned on proposing for PEAR when the
basic API was done and seemed to work.
It is actually done(or at least in Alpha-level, some methods should
probably have their names changed) now, but I haven't got to add PEAR
comments and sync it with the PEAR coding standards and stuff, as well as
add some useful examples (right about now, I only THINK it does everything).
I don't yet have a name for it, so for now it's just named core_object,
since everything extends the core object, but if anyone has any good ideas,
please let me know..
What i have now is the possibility of
1, loading instances:
$o=&new Something();
$o->id=32;
$o->load();
2, loading from lazy-schemes (so you don't have to get ALL data from the
database row, if you don't need it - multiple lazy-loading "classes" are
available per class):
$o=&new Something();
$o->id=32;
$o->load('lazy_classname');
//now $o has only the attributes set that are defined in the lazy_classname
lazyloading-class.
3, saving instances:
$o=&new Something();
$o->id=32;
$o->load();
$o->set('firstname',$o->get('firstname').' changed');
$o->save();
4, deleting instances:
$o=&new Something();
$o->id=32;
$o->unregister();
5, searching using multi-level array style params, eg:
$objects=core_object::locate('something',array(
array(
'firstname',
'=',
'Nezar'
),
'AND',
array(
array(
'lastname',
'like',
'Niels%'
),
'OR',
array(
'lastname',
'like',
'Peters%'
)
)
);
foreach($objects as $o){
print 'object: '.$o->get('id').':
'.$o->get('firstname').'
'.$o->get('lastname').'<br>';
}
- The idea with the static locate method is that you can have as many
levels of the arrays with three items each, and it will be translated into
an SQL string such as
SELECT * from something where ((firstname = 'Nezar') AND ((lastname like
'Niels%') OR (lastname like 'Peters%')))
6, install and deinstall methods, that installs or removes a class' table
from the database
//yields CREATE TABLE SOMETHING(...);
core_object::install('something');
//yields DROP TABLE SOMETHING..
core_object::deinstall('something');
The attributes on a class are defined using an array in the
class-definition, eg:
class something{
...
var $_class_definition=array(
'fields'=>array(
'title'=>array(
'type'=>'string',
'null'=>0
),
'description'=>array(
'type'=>'text',
'null'=>0
)
),
...
);
..and probably it does a few other things that I forgot.
Well, this was sort of an announcement, or at least to tell that I had also
made something that does this, what it doesn't do though, is handle
inheritance between classes... (or well, it does, you just create the new
table adding the fields from the class it enherited from, and the methods
in the inherited class will of course work...but it doesn't do anything
clever)
I'll probably be back, when I've finished the last few little thingeys that
need fixing.
--
Mvh. Nezar Nielsen