我正在构建一个类来将数据保存到数据库,但我不知道如何处理这个问题......
我的项目文件夹是这样的:
+Lib
+Models
+Uddt
-person.php
-uris.php
Main_class.php
Example_usage.php
“Models”文件夹包含 uris.php 文件,该文件具有从数据库表添加和检索信息的方法。
文件夹“Models\Uddt”具有我定义的用户定义数据类型,例如,person.php 如下所示:
class Person {
static $id_category = '2';
public $uri;
public $name;
public $age;
public $sex;
public $address;
public $country;
public function _Name() {
// Connect the property name to the db name
$db_name = 'name_in_db';
// Some more code
}
public function _Age() {
// Connect the property name to the db name
$db_name = 'age_in_db';
// Some more code
}
public function save() {
// do things to save the data to the database
}
}
数据库表如下所示:
Table(uris_details)
uri (PK FK)
id_category (PK FK)
id_data_type (PK FK)
n_text
Table(uris)
uri (PK)
id_uri_state (FK)
要将数据插入数据库,我想使用一个对象,就像这样:
// Instantiate the object
$p = new Person;
// Add the values to the object
$p->uri = 'http://www.fdsddd.com/gfd1';
$p->name = 'John';
$p->age = '27';
$p->sex = 'Male';
$p->address = '10 Street';
$p->country = 'USA';
// Save the data to the database
$p->save();
INSERT 看起来像这样:
INSERT INTO uris (uri, id_uri_state) VALUES ('http://www.fdsddd.com/gfd1', 'active');
INSERT INTO uris_details (uri, id_category, id_data_type, n_text) VALUES ('http://www.fdsddd.com/gfd1', '2', 'name_in_db', 'John');
INSERT INTO uris_details (uri, id_category, id_data_type, n_text) VALUES ('http://www.fdsddd.com/gfd1', '2', 'age_in_db', '27');
INSERT INTO uris_details (uri, id_category, id_data_type, n_text) VALUES ('http://www.fdsddd.com/gfd1', '2', 'sex_in_db', 'Male');
INSERT INTO uris_details (uri, id_category, id_data_type, n_text) VALUES ('http://www.fdsddd.com/gfd1', '2', 'address_in_db', '10 Street');
INSERT INTO uris_details (uri, id_category, id_data_type, n_text) VALUES ('http://www.fdsddd.com/gfd1', '2', 'country_in_db', 'USA');
我的问题:
如何在“Person”类中构建“save()”方法来将数据保存到数据库,有什么想法吗?我必须做一笔交易吗?我正在使用 PostgreSQL,应该更好地创建一个 PlPgSQL 函数来处理事务吗?
给我一些线索。
此致,