php - 族的数据库设计以及如何插入数据库

标签 php mysql database oop

我在编程方面还是个新手,因为这是我进入编程领域的第一年。我在这里读过一些关于设计家谱的其他文章,但我仍然不明白一些细节,所以我需要你的帮助。顺便说一句,我使用 PHP

这是我的简化版本表格

// family leader
<div id="family_leader">
    <input type="text" name="name[]">
    <input type="text" name="ic[]"> // ic is the same like SSN
</div>

// family member
// here input wife or child info
// i use javascript to clone the div so user can add how many family member they have.
<div id="family_member">
    <input type="text" name="name[]">
    <input type="number" name="ic[]"> 
    <input type="text" name="relationship[]">
</div>

我正在考虑以这种方式创建数据库

第一个表将是这样的

用户表

+--------+--------------+--------------+ 
|   id   |     name     |      ic      |
+--------+--------------+--------------+
| id1    | John         | 9357906268   | 
+--------+--------------+--------------+
| id2    | Josh         | 9357222228   | 
+--------+--------------+--------------+

我会再用一张表来记录他们的关系

关系表

  • 没有人是乔什的父亲/母亲
  • 乔什是约翰的父亲

表格看起来像这样

+-------------+--------------+--------------+
|  owner_id   |  family_id   | relationship |
+-------------+--------------+--------------+
|   ic1/id1   |   ic2/id2    |   father     | 
+-------------+--------------+--------------+
|   ic2/id2   |   ic1/id1    |   child      | 
+-------------+--------------+--------------+

如果是单亲,我会这样做,并且我想让这些数据在个人资料中可见

就像

丈夫有1个妻子 父亲有 1 个或多个 child child 有 1 个父亲

到目前为止,这就是我得到的

亲子类

<?php 
/**
* this is my family class
*/
class Family 
{

    function __construct(argument)
    {
        # database construct here
    }

    public function getUser($_POST)
    {
        foreach ($_POST['name'] as $row) {
            # this is how im going to insert the data of user into user table
            $sql1="INSERT INTO User ( name, ic) values (" $_POST[name][i]", "$_POST[ic][i]")" // e.g John, 9357906268 
        }
    }

    public function getRelation($_POST)
    {
        $sql2="INSERT INTO Relationship ( owner_id, family_id, relationship) values ( $_POST['ic'][i], $_POST['ic'][1], $_POST['relationship'][i])"

        /**
        * this is where my problem reside
        * i don't know how to make this function
        */
    }
}

那么我如何正确插入到第二个表中?

我也在考虑在 getRelation 中做一个 switch case

switch ($_POST['relation']) {
    case 'father':
        // in here is if Josh is a father to John then John is a child to Josh.
        break;

    case 'mother':
        # code...
        break;

    // ... more case

    default:
        # code...
        break;
}

或者也许在类中创建更多函数?

 public function isFather($value='')
{
    # code...
}

public function isMother($value='')
{
    #
}

问题是我不确定如何执行函数或 switch case 内部的逻辑来确定用户的关系。

这是数据库设计和 OOP 的正确方法吗?

感谢您的宝贵时间。

最佳答案

and is this the correct way of database design?

小修复:在 User 中定义主键,并在 Relations.owner_idRelations.family_id 中使用它。在这两列上添加 FOREIGN KEY 约束。

也许您需要存储用户的性别,因为有可能 family_leader 实际上是母亲。

如果您有数百万条记录,则将 Relations.relationship 存储为字符串可能会带来性能问题,但对于学习任务来说这是可以的。在MySQL中有一个CHECK Constraint可用于将字段值限制为某个固定的字符串列表,例如检查关系IN('父亲','儿子','女儿','妻子','丈夫')

在回答下一个问题之前,我想指出您的 HTML 代码中的一个小问题:

this is my simplified version form

您在这里混合了 family_leader 和 family_member。最好将它们分开:

<div id="family_leader">
    <input type="text" name="family_leader[name]">
    <input type="text" name="family_leader[ic]"> // ic is the same like SSN
</div>

<div class="family_member"> <!-- don't use id if you're gonna duplicate this div -->
    <input type="text" name="name[]">
    <input type="number" name="ic[]"> 
    <input type="text" name="relationship[]"> <!-- consider using <select> tag here to limit possible relationhips -->
</div>

and OOP?

一个小注意事项:使用 getUsergetRelation 并不好。 “获取”意味着您从某处检索数据而不进行更改。当您的函数插入数据时,您应该将其命名为 saveUser (和 saveRelation)。

现在你问:

the problem is i am not sure how to do the logic inside the function or switch case to determine the relationship of the users.

我建议采用以下 OOP 方法:创建以下 User 类:

<?php

class User {
    public $id;
    public $name;
    public $ic;

    public function save() {
        // save the user to the db and update $this->id with autogenerated id
    }

    public function addChild(User $child) {
        // add to `Relations`: [$this->id, $child->id, 'father']
        // then add to `Relations`: [$child->id, $this->id, 'child']
    }

    public function addWife(User $wife) {
        // check if  this user already has a wife and throw an Exception "Wife already exists"
        // 
        // add to `Relations`: [$this->id, $wife->id, 'husband']
        // then add to `Relations`: [$wife->id, $this->id, 'wife']
    }

    public function addRelative($relationship, User $relative) {
        switch ($relationship) {
            case 'child':
                $this->addChild($relative);
                break;

            case 'wife':
                $this->addWife($relative);
                break;

            default:
                throw new Exception("Unknown relationship:" . $relationship);
                break;
        }
    }
}

然后在Family类中使用以下函数:

public function saveFamily() {
    $family_leader = new User()
    $family_leader->name = $_POST['family_leader']['name'];
    $family_leader->ic = $_POST['family_leader']['ic'];
    $family_leader->save();

    foreach ($_POST['name'] as $i => $name) {
        $family_member = new User();
        $family_member->name = $name;
        $family_member->ic = $_POST['ic'][$i];
        $family_member->save();

        try {
            $family_leader->addRelative($_POST['relationship']['$i'], $family_member);
        } catch (Exception $e) {
            die ('Relationship not added:' . $e->getMessage());
        }
    }
}

关于php - 族的数据库设计以及如何插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38051112/

相关文章:

mysql - 当一张表为空时连接表

Java mysql插入

php - 用表格中的 Logo 替换团队名称

android - 使用 ODBC 和 ASP 将我的 Android 应用程序连接到 MySQL

java - 无法连接到 hsqldb 数据库

php - 如何使用 php sprintf append 正值 "+"但忽略空值

php - Zend_Db 和无缓冲查询

php - 如何将 laravel 6 升级到 7

php - 在提交到 git 之前自动更新内部版本号

database - 超出限制 最大连接数 Postgres