473,394 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

PHP O/R mappers

I was going to ask if there are any O/R (object-relation) mappers for
PHP. I didn't find much on Google until I stumbled across
http://wiki.cc/php/PHP_ORM.

My new question is: Are there any *great* O/R mappers for PHP?

My dream O/R mapper would have great performance, be intuitive to use,
allow me to switch out the DBMS, and remain well supported forever. ;)

Thanks,
David

Jul 17 '05 #1
7 1544
There are no "standard" O/R mappers for the simple reason that only YOU know
the structure of YOUR objects and YOUR relational database, therefore only
YOU know how to map the two structures.

Personally I think that O/R mappers are an extra level of complexity that
can be made totally redundant, therefore they a complete waste of time. By
keeping the structure of my objects exactly the same as the structure of my
relational database I do not need to perform any mapping between the two
structures.

See http://www.tonymarston.co.uk/php-mys...faq.html#faq34 for
another explanation.

--
Tony Marston

http://www.tonymarston.net

"David" <da************@yahoo.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I was going to ask if there are any O/R (object-relation) mappers for
PHP. I didn't find much on Google until I stumbled across
http://wiki.cc/php/PHP_ORM.

My new question is: Are there any *great* O/R mappers for PHP?

My dream O/R mapper would have great performance, be intuitive to use,
allow me to switch out the DBMS, and remain well supported forever. ;)

Thanks,
David

Jul 17 '05 #2
Hello,

on 04/03/2005 01:44 AM David said the following:
I was going to ask if there are any O/R (object-relation) mappers for
PHP. I didn't find much on Google until I stumbled across
http://wiki.cc/php/PHP_ORM.

My new question is: Are there any *great* O/R mappers for PHP?

My dream O/R mapper would have great performance, be intuitive to use,
allow me to switch out the DBMS, and remain well supported forever. ;)


In that case you may want to look at Metastorage.

http://www.meta-language.net/metastorage.html
--

Regards,
Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

PHP Reviews - Reviews of PHP books and other products
http://www.phpclasses.org/reviews/

Metastorage - Data object relational mapping layer generator
http://www.meta-language.net/metastorage.html
Jul 17 '05 #3
NC
David wrote:

I was going to ask if there are any O/R (object-relation)
mappers for PHP. .... My dream O/R mapper would have great performance, be intuitive
to use, allow me to switch out the DBMS, and remain well
supported forever. ;)


In this case, you don't need a mapper. All you need to do
is to run a "SELECT * FROM" query on whatever database you
are using and then *_fetch_object() from the result. This
is supported on MySQL (mysql_fetch_object()), PostgreSQL
(pg_fetch_object()), Microsoft SQL Server (mssql_fetch_object()),
and Oracle (oci_fetch_object()), to name a few.

Cheers,
NC

Jul 17 '05 #4
NC wrote:
David wrote:

I was going to ask if there are any O/R (object-relation)
mappers for PHP.

...
My dream O/R mapper would have great performance, be intuitive
to use, allow me to switch out the DBMS, and remain well
supported forever. ;)


In this case, you don't need a mapper. All you need to do
is to run a "SELECT * FROM" query on whatever database you
are using and then *_fetch_object() from the result.


No - that always returns a pseudo object with no methods which is really an
associative array with slightly different syntax. I think the OP is looking
for a way of defining/storing his own classes and storing/querying objects.

....which is possible using [un]serialize() and __autoload() but requires an
abstraction layer on top of the SQL interface.
My dream O/R mapper would have great performance LOL
be intuitive to use

ROFL

C.

Jul 17 '05 #5
Thanks. A number of you to understand my question, but I'm
disappointed that there were no earthshattering solutions I had been
overlooking.

Tony: I understand your reasoning behind keeping the structure of your
objects the same as your database. However, I would like a tool for
PHP that will automatically generate the objects based on the database
schema. As long as I have a normalized database with nothing too
tricky, an automated tool should be able to do this. (I am using
LLBLGen to do this for me on .NET.) I addition to simply being lazy,
my theory is that the tool is less likely to introduce bugs than I am.

NC: I was not previously aware mysql_fetch_object(), and it looks like
it will do part of what I am looking for: it will return the database
entity stored as an object. However, I want more. I want to be able
to modify one of the objects attributes and then simply execute
something like object.save() or maybe database.save(object) to have it
automatically updated in the database.

Manuel: I saw your tool listed on the PHP wiki. Do you know how
widespread its use is?

Colin: Yeah, basically I want to work with objects in my code but still
store everything in a normal RDBMS. I know there are ways to natively
store objects in a database, but I don't trust those as much as
standard relational databases. I'm glad to have entertained you with
the performance dream.
After doing some more research, it looks like MyObjects is one of the
bigger names in the niche market of PHP O/R mappers. I am also
intrigued by some of the Ruby on Rails stuff but don't really
understand it yet. (Of course, that would be a complete departure from
PHP!)

Thanks,
David

Jul 17 '05 #6
NC
David wrote:

A number of you to understand my question, but I'm
disappointed that there were no earthshattering solutions
I had been overlooking.
If I am not mistaken, most of O/R mappers out there are
code generators, not runtime mappers. Which are you
interested in?
I want to be able to modify one of the objects attributes
and then simply execute something like object.save() or
maybe database.save(object) to have it automatically updated
in the database.


OK, here's a rough MySQL-only draft or a run-time mapper:

class Mapper {
var $host;
var $user;
var $password;
var $db;
var $table;
var $link;
var $data;
var $primaryKey;
var $error;

function Mapper($host, $user, $password, $db, $table) {
$this->link = mysql_connect($host, $user, $password);
if ($this->link == false) {
$this->error = mysql_error();
return false;
}
$db_selected = mysql_select_db($db, $this->link);
if (!$db_selected) {
$this->error = mysql_error();
}
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->db = $db;
$this->table = $table;
$this->error = false;
}

function setPrimaryKey($key = '') {
if ($key == '') {
// Run "SHOW CREATE TABLE {$this->table}" and set
// $this->primaryKey equal to the name of the
// primary key field or, absent that, any key
// field
} else {
$this->primaryKey = $key;
}
}

function populate($key) {
$query = "SELECT * FROM {$this->table} " .
"WHERE {$this->primaryKey} = '$key'";
$result = mysql_query($query);
if ($result == false) {
$this->error = mysql_error();
return false;
} else {
$this->data = mysql_fetch_assoc($result);
return true;
}
}

function save() {
$query = "UPDATE {$this->table} SET ";
$add_comma = false;
$where = '';
foreach ($this->data as $field=>$value) {
if ($add_comma) {
$query .= ", $field='$value'";
} else {
$query .= "$field='$value'";
$add_comma = true;
}
if (strtolower($field) == strtolower($this->primaryKey)) {
$where = " WHERE $field = $value";
}
}
if ($where == '') {
$this->error = 'No primary key found; ' .
'UPDATE will not be attempted.';
return false;
} else {
$query .= $where;
$result = mysql_query($query);
if ($result) {
return mysql_affected_rows();
} else {
$this->error = mysql_error();
return false;
}
}
}

}

Example of usage:

$myMapper = new Mapper('localhost', 'root', 'password',
'myDB', 'myTable');
if ($myMapper->error) {
die('Mapper Error: ' $myMapper->error);
}
$myMapper->setPrimaryKey('ID');
$myMapper->populate(942385);
$myMapper->data['description'] =
"New description for item {$myMapper->data[$myMapper->primaryKey]}";
$myMapper->save();

Cheers,
NC

Jul 17 '05 #7
NC wrote:
David wrote:
A number of you to understand my question, but I'm
disappointed that there were no earthshattering solutions
I had been overlooking.

PhpPeanuts supports polymorphism and navigational queries. It's SQL
generation objects can be selected by the user from a SearchPage. The
results are presented in pages with a limited length. The objects
already can be combined with AND and OR, next version will allow the
user to combine search objects in the search page too.

PhpPeanuts is fast because it has object caching, metadata caching, does
no implicit object copying and because it does not convert data from
MySQL when loading. It rather converts the data to the local format
(localization) when it is represented in the user interface.

PhpPeanuts supports navigation over 1 to n and n to 1 relationship roles
and reflects this in its user interface. You only have to define the
properties, no getters and setters are needed (but will be used if
defined). Next version will support n to m relationships in the user
interface by hiding intermediate relationship table objects. But earth
shattering...

If I am not mistaken, most of O/R mappers out there are
code generators, not runtime mappers. Which are you
interested in?
phpPeanuts does runtime mapping.
I want to be able to modify one of the objects attributes
and then simply execute something like object.save() or
maybe database.save(object) to have it automatically updated
in the database.


You can also just have a form that uses PntObjectSaveAction to convert
and validate the form, set the values on the object, then it calls
save(), and/or on errors, does forward to a page to give the user the
necessary feedback. It works for arbitrary classes that extend
PntDbObject. The forms can be generated automatically by
PntObjectEditDetailsPage.

Greetings,

Henk Verhoeven,
For more info see
http://www.phppeanuts.org/site/index...rsistency.html
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
6
by: sdurity | last post by:
My company is looking at the IdeaBlade RAD/ORM tool (www.ideablade.com) as an aid to building a relatively simple database application (one main developer, me). I have found the ADO.Net and...
1
by: James D. Williams | last post by:
Are there any Open Source tools available for XML to XML mapping using XSL? I should convert one type of XML message to another type. One solution could be XML Mapping tool. Cheers,
21
by: Islamegy® | last post by:
I was spending time to learn the use of strongly typed collection instead of Dataset/datatable and using Enterprise Library Applictations block.. Recently i discovered there is alot of project...
17
by: Aaron Watters | last post by:
I've been poking around the world of object-relational mappers and it inspired me to coin a corellary to the the famous quote on regular expressions: "You have objects and a database: that's 2...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.