Last active
December 6, 2016 04:27
-
-
Save ak-ymst/ccdc4c169dfc9b348c575254b871b232 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__.'/../models.php'; | |
abstract class CardMapper { | |
protected $pdo; | |
public function __construct($pdo) { | |
$this->pdo = $pdo; | |
} | |
/** | |
* IDをキーにCardsクラスのインスタンスを一件取得する | |
* | |
* 取得されるインスタンスの実際のクラスはこのマッパーを継承する | |
* クラスによって異なる | |
* | |
*/ | |
public function find($id, $acc = null) { | |
$row = $this->fetchRowFrom('classtable_cards', $id); | |
$acc->id = $row['id']; | |
$acc->name = $row['name']; | |
return $acc; | |
} | |
/** | |
* 対象テーブルからIDをキーにレコードを一件, 連想配列として取得する | |
* | |
*/ | |
protected function fetchRowFrom($table, $id) { | |
$sql = "SELECT * FROM ".$table." WHERE id = :id"; | |
$params = ['id' => $id]; | |
return $this->fetchRow($sql, $params); | |
} | |
/** | |
* SQLを実行し, 結果を一件だけ連想配列として返す | |
*/ | |
protected function fetchRow($sql, $params) { | |
$stmt = $this->pdo->prepare($sql); | |
$stmt->execute($params); | |
$assoc = $stmt->fetch(PDO::FETCH_ASSOC); | |
if ($assoc === false) { | |
return null; | |
} | |
return $assoc; | |
} | |
/** | |
* モデルの更新を行う | |
* | |
*/ | |
public function update($model) { | |
$sql = "update classtable_cards set name = :name where id = :id"; | |
$params = []; | |
$params['id'] = $model->id; | |
$params['name'] = $model->name; | |
return $this->execute($sql, $params); | |
} | |
protected function execute($sql, $params) { | |
$stmt = $this->pdo->prepare($sql); | |
return $stmt->execute($params); | |
} | |
} | |
/** | |
* アイドル用データマッパー | |
*/ | |
class IdolMapper extends CardMapper { | |
/** | |
* IDをキーにオブジェクトを一件取得する | |
*/ | |
public function find($id, $acc = null) { | |
$row = $this->fetchRowFrom('classtable_idols', $id); | |
if ($row == null) { | |
return null; | |
} | |
$model = ($acc != null) ? $acc : new Idol(); | |
$model->cost = $row['cost']; | |
return parent::find($id, $model); | |
} | |
/** | |
* モデルの更新を行う | |
* | |
*/ | |
public function update($model) { | |
$sql = "update classtable_idols set cost = :cost where id = :id"; | |
$params = []; | |
$params['id'] = $model->id; | |
$params['cost'] = $model->cost; | |
return $this->execute($sql, $params) && parent::update($model); | |
} | |
} | |
/** | |
* トレイナー用データマッパー | |
*/ | |
class TrainerMapper extends IdolMapper { | |
/** | |
* IDをキーにオブジェクトを一件取得する | |
*/ | |
public function find($id, $acc = null) { | |
$row = $this->fetchRowFrom('classtable_trainers', $id); | |
if ($row == null) { | |
return null; | |
} | |
$model = ($acc != null) ? $acc : new Idol(); | |
$model->exp = $row['exp']; | |
return parent::find($id, $model); | |
} | |
/** | |
* モデルの更新を行う | |
* | |
*/ | |
public function update($model) { | |
$sql = "update classtable_trainers set exp = :exp where id = :id"; | |
$params = []; | |
$params['id'] = $model->id; | |
$params['exp'] = $model->exp; | |
return $this->execute($sql, $params) && parent::update($model); | |
} | |
} | |
/** | |
* アイテム用データマッパー | |
*/ | |
class ItemMapper extends CardMapper { | |
/** | |
* IDをキーにオブジェクトを一件取得する | |
*/ | |
public function find($id, $acc = null) { | |
$row = $this->fetchRowFrom('classtable_items', $id); | |
if ($row == null) { | |
return null; | |
} | |
$model = ($acc != null) ? $acc : new Item(); | |
$model->repairPoint = $row['repair_point']; | |
return parent::find($id, $model); | |
} | |
/** | |
* モデルの更新を行う | |
* | |
*/ | |
public function update($model) { | |
$sql = "update classtable_items set repair_point = :repair_point where id = :id"; | |
$params = []; | |
$params['id'] = $model->id; | |
$params['repair_point'] = $model->repairPoint; | |
return $this->execute($sql, $params) && parent::update($model); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment