dra87370 2011-02-14 14:07 采纳率: 100%
浏览 12

PHP类。 如何构建将数据保存到数据库的方法

I'm building a class to save data to the Database, but I'm without ideas on how to deal with this...

I have my project folders like this:

+Lib
+Models
    +Uddt
    -person.php
-uris.php
Main_class.php
Example_usage.php

The folder "Models" have the uris.php file that have methods to add and retrive information from the database tables.

The folder "Models\Uddt" have the User Defined Data Types that I have defined, for example, the person.php looks like this:

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
    }

}

The database tables looks like this:

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)

To insert the data to the database I want to use an object, just like this:

// 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(); 

The INSERTs will look like this:

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');

My question:

How can I structure the "save()" method in the "Person" Class to save the data to the Database, any ideas? I must to do a transaction? I'm using PostgreSQL, shoul be better to create a PlPgSQL function to deal with the transaction?

Give me some clues.

Best Regards,

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?