2012-02-14 88 views
0

我將首先告訴你我想要我的代碼做什麼以及它是如何實現的。需要給會員添加一個房屋ID

成員創建一個帳戶,然後創建一個房子,因此成爲房屋管理員。管理員必須能夠邀請其他用戶通過電子郵件加入該房屋。當用戶發送電子郵件時,我希望電子郵件附有house_id,以便當收到電子郵件的會員填寫註冊表單時,程序已經知道該會員屬於哪個房屋並可以將其保存爲所以在數據庫中。我希望我有道理。

到目前爲止,只要管理員創建房屋,他們的member_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

對不起,但這是很多代碼,只是能夠幫助您 – 2012-02-14 14:34:16

+0

您的數據結構意味着一個成員可以在一個(而且只有一個)房子。您可以更靈活地使用與這兩個獨立實體相關的'house_members'鏈接表。 – 2012-02-14 14:35:00

回答

0

看起來你是在正確的軌道上。我認爲你需要在成員類中調用這個函數:

public function setHouseID($h) { $this->house_id = $h; } 

從House類的構造函數中。如前所述,除非每個房屋只需要一名成員,否則您將需要一張單獨的子表來存儲houseid/memberid配對。

+0

一個成員只能是一個房子的一部分,這就是預期的結構。 我正在嘗試打電話給他,但是在創建房屋時,ID只是被創建,然後我需要該ID在同一時間添加到成員表中,如果您知道我的意思嗎? – suzaane 2012-02-14 14:51:18

+0

@Topener您能否指點我正確的方向?我會很感激。 – suzaane 2012-02-14 16:55:29

+0

你必須先插入房子,獲得id,然後插入到成員中。 – 2012-02-14 17:43:29