【问题标题】:database design of a family and how to insert into database一个家族的数据库设计以及如何插入数据库
【发布时间】:2016-06-27 10:05:14
【问题描述】:

我还是编程新手,因为这是我在编程领域的第一年。我在这里读过一些关于设计家谱的其他帖子,但我仍然不明白一些细节,所以我需要你的帮助。顺便说一句,我使用 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 个或多个孩子 孩子有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 案例

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的正确方法吗?

感谢您的宝贵时间。

【问题讨论】:

    标签: php mysql database oop


    【解决方案1】:

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

    小修复:在User 中定义主键并在Relations.owner_idRelations.family_id 中使用它。在这 2 列上添加 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());
            }
        }
    }
    

    【讨论】:

    • 我这样使用家庭课程吗? $family = new family(); $family = saveFamily(); 班级会得到 $_POST 吗?
    • Family 类中定义saveFamily() 并使用$family = new Family(); $family-&gt;saveFamily(); 将来您可能会寻找一些不错的技术,例如method chaining
    • 你认为我可以用我目前所做的更新我的问题吗?我认为我的用户类有问题
    • 最好提出新问题并仅包含有问题部分的代码。此问题已包含过多主题,即将被标记为“过于宽泛”
    • 是的,我也是这么认为的,因为您的回答已经回答了我当前的问题。
    猜你喜欢
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多