Is it possible to insert Object in a database?
Is it po开发者_StackOverflow中文版ssible to insert Object in a database? (Asked in PHP interview.)
Yes, it is possible, using serialize()
.
But the answer hugely depends on context.
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
If you are talking about PHP object, it's not possible to insert an object into 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 database. 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.
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.
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.
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.
You can also be a wise-ass:
You: "You could take the PHP object from memory and port it to either Java or .NET and then use a Versant object database to store the object."
精彩评论