家庭的数据库设计以及如何插入数据库

时间:2016-06-27 10:05:15

标签: 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   | 
+--------+--------------+--------------+

我将为他们的关系提供另一张表

关系表

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

表格看起来像这样

+-------------+--------------+--------------+
|  owner_id   |  family_id   | relationship |
+-------------+--------------+--------------+
|   ic1/id1   |   ic2/id2    |   father     | 
+-------------+--------------+--------------+
|   ic2/id2   |   ic1/id1    |   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='')
{
    #
}

问题是我不知道如何在函数内部做逻辑或切换案例以确定用户的关系。

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

谢谢你的时间。

1 个答案:

答案 0 :(得分:2)

  

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

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

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

如果您拥有数百万条记录,则将Relations.relationship存储为字符串可能会产生性能问题,但对于学习任务而言,这是正常的。在MySQL中有一个CHECK Constraint可用于将字段值限制为某些固定的字符串列表,例如CHECK relationship IN('father', 'son', 'daughter','wife','husband')

在回答下一个问题之前,我想在HTML代码中注意一个小问题:

  

这是我的简化版本

您在这里混合了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>
  

和OOP?

小注意:使用getUsergetRelation并不好。 “获取”意味着您无需更改即可从某个位置检索数据。当您的函数插入数据时,您应该将其命名为saveUser(和saveRelation)。

现在你问:

  

问题是我不知道如何在函数内部执行逻辑   或切换案例以确定用户的关系。

我建议遵循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());
        }
    }
}