我在编程方面还是个新手,因为这是我进入编程领域的第一年。我在这里读过一些关于设计家谱的其他文章,但我仍然不明白一些细节,所以我需要你的帮助。顺便说一句,我使用 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 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的正确方法吗?
感谢您的宝贵时间。
这是数据库设计的正确方法吗?
小修复:在
User
中定义主键并在 Relations.owner_id
和 Relations.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>
和面向对象编程?小提示:使用
getUser
和
getRelation
不好。 “获取”意味着您从某处检索数据而不进行更改。当您的函数插入数据时,您应该将其命名为
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());
}
}
}