Ati Hcm
Ati Hcm

Reputation: 21

best format to store php object to postgresql database

I wonder what is the best format to store PHP objects to database, an object have multiple parameters and methods, XML or binary or other formats?

for example :

$me = new Person();
$me->code= '007';
$me->name='antoine';
$me->store(); //this object is now stored into postgresql in Binary format or XML or ... you tell me !
//after few days : 
$object = $database->retrievePersonByCode('007');
echo "my name is".$object->name;

How to do that ?

Upvotes: 1

Views: 1129

Answers (3)

greg
greg

Reputation: 3495

This question is the first stone of the existence of ORM and object oriented persistence layers.

Either you store PHP objects as is in a serialized way (text, json, xml, whatever...) and you loose the ability to perform queries on a particular attribute of your objects (ie looking for all Personn objects younger than 18 by example). Changing one atribute in your instance means updating the whole object.

Or you use a code layer that transposes your PHP objects to real tables or sets in the database. There is the ORM familly that uses abstraction layer (Doctrine, Propel and thousands more...) and there is the OMM familly that just uses Postgres (Pomm).

Upvotes: -1

Brent Baisley
Brent Baisley

Reputation: 12721

Personally I've standardized on json since any language can read it back easily. Plus it's human readable (not binary) and not nearly as verbose as XML. Since Postgres actually supports json as a data type, that is an additional bonus.

Upvotes: 0

pajaja
pajaja

Reputation: 2202

You will need to serialize it first. PHP has a serialize() function which you can use. But you should pay attention to this when using it:

Object's private members have the class name prepended to the member name; protected members have a '*' prepended to the member name. These prepended values have null bytes on either side.

Depending on the size of the object you wish to serialize you can use TEXT, MEDIUMTEXT or even LONGTEXT.

Upvotes: 2

Related Questions