Jump to content

php serialize


chaiwei

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.