chaiwei Posted July 16, 2009 Share Posted July 16, 2009 Hi, I was thinking using serialization to reduce my database field. eg. I got a dog object with attribute (name , age , owner). So I got a database table ( dog_table ) with field (id , name , age ,owner). <? class dog { private $name; private $age; private $owner; function __construct($in_name="unnamed",$in_age="0",$in_owner="unknown") { $this->name = $in_name; $this->age = $in_age; $this->owner = $in_owner; } function getage() { return ($this->age * 365); } function getowner() { return ($this->owner); } function getname() { return ($this->name); } function save() { mysql_query("INSERT INTO dog_table VALUES( null , 'getname()' , 'getage()' , 'getowner()' )"); } } ?> $bob = new dog('bob' , 2 , 'john'); $bob->save(); with serialization, I can change my table structure to: database table ( dog_table ) field (id , dogObject ) remain the class, add one more function -> dogObjSerialize() and modify the save function. <? class dog { private $name; private $age; private $owner; private $dogObj; function __construct($in_name="unnamed",$in_age="0",$in_owner="unknown") { $this->name = $in_name; $this->age = $in_age; $this->owner = $in_owner; } function getage() { return ($this->age * 365); } function getowner() { return ($this->owner); } function getname() { return ($this->name); } function dogObjSerialize($obj){ $this->dogObj = $obj; } function save() { mysql_query("INSERT INTO dog_table VALUES( null , '$this->dogObj' )"); } } ?> $bob = new dog('bob' , 2 , 'john'); $bob->dogObjSerialize(serialize($bob)); $bob->save(); Is this way better? Link to comment https://forums.phpfreaks.com/topic/166212-php-serialize/ Share on other sites More sharing options...
Daniel0 Posted July 16, 2009 Share Posted July 16, 2009 No, for a table to be even 1NF (1st normal form) in database normalization it's a requirement that each field is atomic, that's to say it contains only one thing and it cannot be broken down any further. Link to comment https://forums.phpfreaks.com/topic/166212-php-serialize/#findComment-876489 Share on other sites More sharing options...
chaiwei Posted July 17, 2009 Author Share Posted July 17, 2009 hmmm,not understand. because from what I see, It is easier to retrieve the value. example, mysql_query("INSERT INTO dog_table VALUES( null , '$this->dogObj' )"); $dogObj = mysql_fetch_array(mysql_query("SELECT * FROM dog_table WHERE id=1")); $dog = unserialize($dogObj['dogObject']); like this I can straight use $dog->getname(); I don't have to : mysql_query("INSERT INTO dog_table VALUES( null , '$this->dogObj' )"); $dogObj = mysql_fetch_array(mysql_query("SELECT * FROM dog_table WHERE id=1")); $this->name=$dogObj['name']; $this->age=$dogObj['age']; Link to comment https://forums.phpfreaks.com/topic/166212-php-serialize/#findComment-876884 Share on other sites More sharing options...
Daniel0 Posted July 17, 2009 Share Posted July 17, 2009 But how would you then retrieve all dogs that are older than 2 years? That's why atomicity is a requirement for normalized database schemas. In the dog table, one row should already represent one dog, so having a dog inside the dog doesn't make sense. If you want to represent the dog as an object in PHP you could write a simple ORM (object-relational mapper) or use an existing one such as Doctrine. Link to comment https://forums.phpfreaks.com/topic/166212-php-serialize/#findComment-876898 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.