Reputation: 966
My question is how should i be inserting php class objects into the database? If i have
Cat.php
class Cat{
private $name;
public function setName($name){
$this->name = $name
}
public function database(){
....$this->name;
//Insert stuff into db
}
}
Somefile.php
if($_POST['catname']){
//This way?
$obj1 = new Cat();
$obj1->setName($_POST['catname']);
$obj1->database();
//Or this way
//Insert Cat directly into the db without creating an object of Cat
}
And i want to insert this cat into the database. Would it be best to create a function within cat that gets $this->name or before i create the object should i just insert it into the database?
Upvotes: 1
Views: 9502
Reputation: 9910
You can use serialize
, and if you have a good autoloader, you will not have to worry about the class not existing when you unserialize.
Also note the 2 magic functions that you can use:
__sleep()
- custom code to use when serializing an object
__wakeup()
- custom code when unserializing an object
PS:
Replace:
$obj1->setName = $_POST['catname'];
with
$obj1->setName($_POST['catname']);
References:
Upvotes: 2
Reputation: 2441
Take a look at serialize and unserialize, you can store the serialised string in your database if required - here's a quick example.
<?php
class Cat {
protected
$name;
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
return $this;
}
}
$cat = new Cat;
$cat->setName('Tiddles');
$string = serialize($cat);
$cat = unserialize($string);
var_dump($cat);
/*
object(Cat)#2 (1) {
["name":protected]=>
string(7) "Tiddles"
}
*/
Anthony.
Upvotes: 0
Reputation: 4541
Normally there are several layers in your project and one of this layer is the DAO (Data Access Object). In this class you link your application to a database. this layer has to save your object into the database via a save method for example...
The day you wanna change the database you just rewrite this layer
To be really ideal, the object that is present in the DAO layer is different from the model object (CAT) : it is a persistent Object that looks like the table in the database
Upvotes: 1
Reputation: 1427
The easiest way of doing it would be to serialize your class to string.
$string = serialize($cat);
http://php.net/manual/en/language.oop5.serialization.php
But this way you will create lots of overhead in your database.
The best way to do it would be to have the relevant fields in your table and just save the corresponding values. You can easily create a function which reads / saves your class to a table. This would make your data much more portable and exchangeable.
Upvotes: 2
Reputation: 43700
You should have a separate object that takes an instance of cat and puts the data into the database. You may want to have a method of getVitalData
which provides all the data that should go into the database (you may eventually have more than just name).
Each class should have one responsibility
Upvotes: 1
Reputation: 3127
Ideally, the object itself should deal with saving itself to the database. This way, your logic doesn't need to know what cat
is, or where it is saving itself. The cat
class should be self-contained, and this means if you want to change DB types or anything else in the future, you can make it in the class without affecting your logic.
Upvotes: 1