howto put an object into a table

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
grisha
Forum Newbie
Posts: 3
Joined: Sat Nov 06, 2004 4:51 pm
Contact:

howto put an object into a table

Post by grisha »

hello. i have a table with a laaaarge (about 35) number of fields. now, the problem is that i'm too lazy to program all the stuff by hand:)))

i use a class that has the same fields as the table....

i can read an object from the table using mysql_fetch_object,but....

i wonder if there isn't a method that can do this thing in the other direction: write an object to the table! its a great problem, because i'll have many such tables with different fields, and programming all this by hand will possibly take pretty much time and lead to bugs....... :roll:
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

[php_man]serialize[/php_man]
grisha
Forum Newbie
Posts: 3
Joined: Sat Nov 06, 2004 4:51 pm
Contact:

Post by grisha »

patrikG wrote:[php_man]serialize[/php_man]
maybe i'm very stupid, but i dont understand what you mean.

great thanxx if you could explain what you mean in a few words., since if i knew what youre saying, i woldnt ask=))
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

PHP Manual wrote:serialize is useful for storing or passing PHP values around without losing their type and structure.
You can convert any variable (except a resource-type) into a string which you can store in a database. If you unserialize it, you'll discover that neither value nor structure of the original variable is lost. Try it out.

And please do read the manual.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Re: howto put an object into a table

Post by BDKR »

grisha wrote: i wonder if there isn't a method that can do this thing in the other direction: write an object to the table! its a great problem, because i'll have many such tables with different fields, and programming all this by hand will possibly take pretty much time and lead to bugs....... :roll:
I really don't jive with this approach to dealing with objects and data, but, it popped into my head earlier. If you can query the database for information about a table, then can't you dynamically generate logic associated with that table? (You can see I'm not really giving an answer here as much as I'm just trying to get you to think). Also, can't you iterate over the object returned by mysql_fetch_object()? Think about this and using a queries like...

1) DESCRIBE `your_table`
2) SHOW COLUMNS FROM `your_table`

Read the manual and peruse the mysql functions.

Cheers,
BDKR
grisha
Forum Newbie
Posts: 3
Joined: Sat Nov 06, 2004 4:51 pm
Contact:

Post by grisha »

patrikG wrote:
PHP Manual wrote:serialize is useful for storing or passing PHP values around without losing their type and structure.
You can convert any variable (except a resource-type) into a string which you can store in a database. If you unserialize it, you'll discover that neither value nor structure of the original variable is lost. Try it out.

And please do read the manual.
ah,what the hell,now i see. thanx a lot. i didn't realise what serializing had with the context to do, but now.....a great idea:))

greetz
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Btw: you may want to have a look at this: http://www.zend.com/php5/contest/contes ... 6&single=1

A project called "MyObjects":
Project Description wrote: MyObjects is a set of tools for database schema generation, class generation and object persistence. It will make the php developers life much easier with generating code for database operations, data validation, xml mapping and so on. Most of the database operations are done transparently using the built in runtime classes of MyObjects.
Very nifty!
Post Reply