私は自分のコードで何をしたいのか、どのようにするのかを教えて始めます。メンバーに家のIDを追加する必要があります
メンバーはアカウントを作成してから家を作成するため、家の管理者になります。管理者は、他のユーザーを招待して、電子メールで家に入ることができる必要があります。ユーザーに電子メールが送信されると、電子メールにhouse_idが添付されていて、電子メールを受け取ったメンバーが登録フォームに記入すると、そのメンバーはメンバーがどの家にいるかを既に知っていて、データベース内で私は理にかなっているといい。
これまでのところ、管理者が家を作成したときに、そのメンバーIDが家に割り当てられていれば、私は働くことができます。だからあなたは、家の管理者が誰であるかを知ることができます。 house_idをメンバーのテーブルに追加することはできません。そのためメンバーテーブルでは、メンバーがどの家の一部かを見ることができます。
メンバークラス、メンバーDAO、ハウスクラス、houseDAOがあります。
私は非常に困惑しているので、誰もがこれを手伝ってくれるのだろうかと思っていました。
申し訳ありません申し訳ありませんが私はこの言語には非常に新しいので、十分にはっきり説明していません。 ありがとうございます。
メンバークラス
<?php
class Member {
private $id;
private $first_name;
private $last_name;
private $email;
private $birthday;
private $password;
private $house_id;
public function __construct($i, $fName, $lName, $eml, $birth, $pwd, $house_id) {
$this->id = $i;
$this->first_name = $fName;
$this->last_name = $lName;
$this->email = $eml;
$this->birthday = $birth;
$this->password = $pwd;
$this->house_id = $house_id;
}
public function getId() { return $this->id; }
public function getFN() { return $this->first_name; }
public function getLN() { return $this->last_name; }
public function getEmail() { return $this->email; }
public function getBirthday() { return $this->birthday; }
public function getPassword() { return $this->password; }
public function getHouseID() { return $this->house_id; }
public function setId($i) { $this->id = $i; }
public function setFN($fn) { $this->first_name = $fn; }
public function setLN($ln) { $this->last_name = $ln; }
public function setEmail($n) { $this->email = $n; }
public function setBirthday($b) { $this->birthday = $b; }
public function setPassword($p) { $this->password = $p; }
public function setHouseID($h) { $this->house_id = $h; }
}
?>
MemberDAO
<?php
require_once 'DAO.php';
class MemberDAO extends DAO {
public function __construct() {
parent::__construct();
}
public function insert($member) {
if (!isset($member)) {
throw new Exception("Member required");
}
$sql = "INSERT INTO Member(first_name, last_name, email, birthday, password) VALUES (?, ?, ?, ?, ?)";
$params = array($member->getFN(), $member->getLN(), $member->getEmail(), $member->getBirthday(), $member->getPassword());
$stmt = $this->link->prepare($sql);
$status = $stmt->execute($params);
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not save member: " . $errorInfo[2]);
}
$sql = "SELECT LAST_INSERT_ID()";
$stmt = $this->link->prepare($sql);
$status = $stmt->execute();
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not retrieve new member's id: " . $errorInfo[2]);
}
$row = $stmt->fetch();
$id = $row[0];
$member->setId($id);
}
public function delete($member) {
if (!isset($member)) {
throw new Exception("Member required");
}
$id = $member->getId();
if ($id == null) {
throw new Exception("Member id required");
}
$sql = "DELETE FROM Member WHERE id = ?";
$params = array($member->getId());
$stmt = $this->link->prepare($sql);
$status = $stmt->execute($params);
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not delete member: " . $errorInfo[2]);
}
}
public function update($member) {
if (!isset($member)) {
throw new Exception("Member required");
}
$id = $member->getId();
if ($id == null) {
throw new Exception("Member id required");
}
$sql = "UPDATE Member SET first_name = ?, last_name = ?, email = ?, birthday = ?, password = ? WHERE id = ?";
$params = array($member->getFN(), $member->getLN(), $member->getEmail(), $member->getBirthday(), $member->getPassword(), $member->getId());
$stmt = $this->link->prepare($sql);
$status = $stmt->execute($params);
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not update member: " . $errorInfo[2]);
}
}
public function getMember($id) {
$sql = "SELECT * FROM Member WHERE id = ?";
$params = array($id);
$stmt = $this->link->prepare($sql);
$status = $stmt->execute($params);
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not retrieve member: " . $errorInfo[2]);
}
$member= null;
if ($stmt->rowCount == 1) {
$row = $stmt->fetch();
$id = $row['id'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$email = $row['email'];
$birthday = $row['birthday'];
$pwd = $row['password'];
$member = new Member($id, $first_name, $last_name, $email, $birthday, $pwd);
}
return $member;
}
public function getMembers() {
$sql = "SELECT * FROM Member";
$stmt = $this->link->prepare($sql);
$status = $stmt->execute();
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not retrieve members: " . $errorInfo[2]);
}
$members = array();
$row = $stmt->fetch();
while ($row != null) {
$id = $row['id'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$email = $row['email'];
$birthday = $row['birthday'];
$pwd = $row['password'];
$member = new Member($id, $first_name, $last_name, $email, $birthday, $pwd);
$members[$id] = $member;
$row = $stmt->fetch();
}
return $members;
}
}
?>
ハウスクラス
<?php
class House {
private $id;
private $house_name;
private $no_members;
private $member_id;
public function __construct($i, $house_name, $no_members, $m_id) {
$this->id = $i;
$this->house_name = $house_name;
$this->no_members = $no_members;
$this->member_id = $m_id;
}
public function getId() { return $this->id; }
public function getHouseName() { return $this->house_name; }
public function getNoMem() { return $this->no_members; }
public function getMember_id() { return $this->member_id; }
public function setId($i) { $this->id = $i; }
public function setHouseName($hn) { $this->HouseName = $hn; }
public function setNoMem($nm) { $this->no_members = $nm; }
public function setMember_id($mID) { $this->member_id = $mID; }
}
?>
HouseDAO
<?php
require_once 'DAO.php';
class HouseDAO extends DAO {
public function __construct() {
parent::__construct();
}
public function insert($house) {
if (!isset($house)) {
throw new Exception("House required");
}
$sql = "INSERT INTO House(house_name, no_members, member_id) VALUES (?, ?, ?)";
$params = array($house->getHouseName(), $house->getNoMem(), $house->getMember_id());
$stmt = $this->link->prepare($sql);
$status = $stmt->execute($params);
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not save House: " . $errorInfo[2]);
}
$sql = "SELECT LAST_INSERT_ID()";
$stmt = $this->link->prepare($sql);
$status = $stmt->execute();
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not retrieve new House's id: " . $errorInfo[2]);
}
$row = $stmt->fetch();
$id = $row[0];
$house->setId($id);
}
public function delete($house) {
if (!isset($house)) {
throw new Exception("House required");
}
$id = $house->getId();
if ($id == null) {
throw new Exception("House id required");
}
$sql = "DELETE FROM House WHERE id = ?";
$params = array($house->getId());
$stmt = $this->link->prepare($sql);
$status = $stmt->execute($params);
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not delete House: " . $errorInfo[2]);
}
}
public function update($house) {
if (!isset($house)) {
throw new Exception("House required");
}
$id = $house->getId();
if ($id == null) {
throw new Exception("House id required");
}
$sql = "UPDATE House SET house_name = ?, no_members = ?, member_id = ? WHERE id = ?";
$params = array($house->getHouseName(), $house->getNoMem(), $house->getMember_id());
$stmt = $this->link->prepare($sql);
$status = $stmt->execute($params);
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not update House: " . $errorInfo[2]);
}
}
public function getHouse($id) {
$sql = "SELECT * FROM House WHERE id = ?";
$params = array($id);
$stmt = $this->link->prepare($sql);
$status = $stmt->execute($params);
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not retrieve House: " . $errorInfo[2]);
}
$house = null;
if ($stmt->rowCount == 1) {
$row = $stmt->fetch();
$id = $row['id'];
$member_id = $row['member_id'];
$house_name = $row['house_name'];
$no_members = $row['no_members'];
$house = new HouseDAO($id, $house_name, $no_members, $member_id);
}
return $house;
}
public function getHouses() {
$sql = "SELECT * FROM House";
$stmt = $this->link->prepare($sql);
$status = $stmt->execute();
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not retrieve houses: " . $errorInfo[2]);
}
$houses = array();
$row = $stmt->fetch();
while ($row != null) {
$id = $row['id'];
$house_name = $row['house_name'];
$no_members = $row['no_members'];
$member_id = $row['member_id'];
$house = new House($i, $house_name, $member_id, $m_id);
$houses[$id] = $house;
$row = $stmt->fetch();
}
return $houses;
}
}
?>
申し訳ありませんが、これはあなたを助けることができるだけのコードです。 –
あなたのデータ構造は、メンバーが1つの唯一の家に入ることを意味します。これらの2つの別個のエンティティに関連する 'house_members'リンクテーブルをもっと柔軟に使うことができます。 –