Asked in PHP interview, Is it possible to insert Object in database?
Yes, it is possible, using serialize()
.
But the answer hugely depends on context.
If you are talking about PHP object , its not possible to insert object in to the database.to store it into the database you first need to serialize it and un-serialize it at the time of use
That question is extremely vague and undefined. I mean on one hand, you can serialize an object and insert it into the database. However, "inserting an object" depends on context. For example, is he referring to the fact that you can call an sql statement like:
$a = new object();
// this makes no sense
$sql = "Insert into table_name values($a);";
If that is what he's asking, then the answer is no, you can't do that unless you override the __toString() method which returns the values to be inserted into the db. Even then, the class isn't well defined because you're saying that __toString() will be a database values, but yet that isn't clearly defined or implied without referring to the class object itself.
In any case though, if an Object represents database tables and so forth, then you want to separate each item based on responsibilities. For example, you can have object A which defines the database table, and then have a "manager" object B which inserts, updates, deletes, and select the item from the database. For example:
// NOT REAL PHP CODE....
class Table_Name
{
// define primary keys, foreign keys, and attributes of the table.
private $column1;
public function setColumn1($value);
public function getCOlumn1();
}
class Table_Name_Manager
{
public function insert(Table_Name $obj);
public function delete(Table_Name $obj);
public function select(Table_Name $obj);
}
The above makes the most sense to me because it clearly defines the behaviors that you'd expect. You can simply use a manager to get items off the database, and modify the object, then you can call the manager again and insert, update, or delete.
Short answer: no, it's not possible
Long answer: You cannot insert Object directly, you have to serialize it before storing, for example using built-in serialize()
function or some custom XML serializer. Then you can put it in some TEXT
or BLOB
column.
Correct answer: To put objects in database, use some ORM (Object-Relational Mapping) tool. Most MVC frameworks have ORM built-in, most commonly utilizing Active Record pattern.
yes you can, you just need to serialize it into an appropiate format. The whole point of an open ended question such as that, is to find a way of doing what is being asked.
nobody suggested OR-mappers? interesting …
if you create your database tables so that they represent objects, you can use an object-relational mapper to store the values of your object members to the database.
for php there exists propel and many others
It should be noted that storing serialized objects into relational databases contradicts virtually all database normalization rules and can be considered very bad practice.
To store objects in a database you should use object relational mappers like Doctrine or Propel.
Serialized objects can be stored in your filesystem though. That's ok.