3 Replies - 13221 Views - Last Post: 21 June 2008 - 09:02 PM Rate Topic: -----

#1 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Saving php objects to database

Posted 21 June 2008 - 12:49 PM

I've been wondering about this for a while now, would it be possible to save a whole class object to a db? instead of individual credentials? I know in c++ we use to save the objects to .dat file or whatever kind of file you would like, and im sure I can do the same with php but how about saving your instance of a class to a db in php? Anybody ever done this? or know/think they can do this?

Thanks,
Musya
Is This A Good Question/Topic? 0
  • +

Replies To: Saving php objects to database

#2 Addiction2Code  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 19-December 06

Re: Saving php objects to database

Posted 21 June 2008 - 12:54 PM

I think you would use the serialize() function, and dump the data to the database, then use the unserialize() function to, well, unserialize the data. Kinda like python's Pickle class. I've never used this with a database though, its worth a shot.

An Example Would Probably Look Something Like This.
$the_data = addslashes(serialize($the_data));
mysql_query("INSERT INTO table (class) VALUES ('$data')")

//Then To SELECT...

link = mysql_query("SELECT class FROM table");
$row = mysql_fetch_array($link)
$data = unserialize($row['class'])



This is just working with a one row table, you could make classes for each table and insert your serialized data, or you could do this with multiple rows/tables, (obviously)

This post has been edited by Addiction2Code: 21 June 2008 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

#3 joeyadms  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 178
  • Joined: 04-May 08

Re: Saving php objects to database

Posted 21 June 2008 - 01:01 PM

I have a tutorial in the tutorials section for serilizing objects.

The thing to remmber is you still need the class file (or code) included to successfully unserialize the object and use it.

Here is that tut:
http://www.dreaminco...wtopic54245.htm
Was This Post Helpful? 0
  • +
  • -

#7 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: Saving php objects to database

Posted 21 June 2008 - 09:02 PM

View Postmusya, on 21 Jun, 2008 - 02:49 PM, said:

I've been wondering about this for a while now, would it be possible to save a whole class object to a db? instead of individual credentials?

Yes, as others have described, you can save a serialized object to the database (with some limitations - you can't serialize resources like file handles and database connections). The real question is: is this a good idea?

More often than not, the answer is: no. Relational databases are not flat files. Using them as such completely defeats the purpose. There are many reasons for this. Here are a few, in no particular order.

1) Data integrity - Relational databases can enforce data integrity - data type, not null constraints, uniqueness constraints, foreign key constraints, and even arbitrary constraints via CHECK clauses (unless you're using MySQL, of course). When you store all your data in a single text column, all that goes down the drain.

2) Easier access - With a standard relational table, it's easy to see and manipulate the structure of the data. You can run ad hoc queries, use reporting packages, and easily write other programs in other languages that access the same tables. When you're dealing with a serialized object blob, only the original object knows how to interpret that. Want to see how many objects have property foo? With the serialization approach, this becomes a major chore. WIth a standard relational approach, it's completely trivial.

3) Performance - Databases have indexes, query optimizers, and all sorts of other tricks for getting you your data fast. They can't do that with a big, serialized string. If you ever need to do anything except retreive a single, self-contained object, your performance is going to suffer. And if you think retreiving all the rows and doing your search in code will be just as fast, you're crazy.

So I would encourage you to think about why you want to do this. There are valid reasons for doing this, but not that many. If you're just trying to save some coding, then it's a bad idea. Unless this is a throw-away application, you'll just end up causing yourself more work than you save. If you want to have a simple way to store your objects, a better approach would be to use relational tables and an object-relational mapper of some sort. That gets you the nice, simple $object->save() syntax for persisting your data, while maintaining all the benefits of a relational database.
Was This Post Helpful? 1

Page 1 of 1