Created
December 6, 2016 03:45
-
-
Save ak-ymst/905cf5fdac53bd8cd7a590ef9d8fbb9b 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 | |
abstract class CardMapper { | |
protected $pdo; | |
public function __construct($pdo) { | |
$this->pdo = $pdo; | |
} | |
/** | |
* IDをキーにCardsクラスのインスタンスを一件取得する | |
* | |
* 取得されるインスタンスの実際のクラスはこのマッパーを継承する | |
* クラスによって異なる | |
* | |
*/ | |
public function find($id) { | |
$row = $this->fetchRowFrom($this->getTable(), $id); | |
if ($row == null) { | |
return null; | |
} | |
$model = $this->createNewInstance(); | |
$this->fill($row, $model); | |
return $model; | |
} | |
/** | |
* 対象テーブルからIDをキーにレコードを一件, 連想配列として取得する | |
* | |
*/ | |
protected function fetchRowFrom($table, $id) { | |
$sql = "SELECT * FROM ".$table." WHERE id = :id"; | |
$params = ['id' => $id]; | |
return $this->fetchOne($sql, $params); | |
} | |
/** | |
* SQLを実行し, 結果を一件だけ連想配列として返す | |
*/ | |
protected function fetchOne($sql, $params) { | |
$stmt = $this->pdo->prepare($sql); | |
$stmt->execute($params); | |
$assoc = $stmt->fetch(PDO::FETCH_ASSOC); | |
if ($assoc === false) { | |
return null; | |
} | |
return $assoc; | |
} | |
/** | |
* 各マッパークラスに対応するモデルオブジェクトを生成する | |
*/ | |
abstract protected function createNewInstance(); | |
/** | |
* 各マッパークラスが対応するテーブル名を返す | |
*/ | |
abstract protected function getTable(); | |
/** | |
* DBレコードから対応する値をモデルにセットする | |
* | |
*/ | |
protected function fill($row, $model) { | |
$model->id = $row['id']; | |
$model->name = $row['name']; | |
} | |
/** | |
* モデルの更新を行う | |
* | |
*/ | |
abstract public function update($model); | |
/** | |
* レコードを返さないSQLを実行する | |
* | |
*/ | |
protected function execute($sql, $params) { | |
$stmt = $this->pdo->prepare($sql); | |
return $stmt->execute($params); | |
} | |
} | |
/** | |
* Idolモデル用Mapper | |
* | |
*/ | |
class IdolMapper extends CardMapper { | |
/** | |
* 各マッパークラスに対応するモデルオブジェクトを生成する | |
*/ | |
protected function createNewInstance() { | |
return new Idol(); | |
} | |
/** | |
* 各マッパークラスが対応するテーブル名を返す | |
*/ | |
protected function getTable() { | |
return 'leaftable_idols'; | |
} | |
/** | |
* DBレコードから対応する値をモデルにセットする | |
* | |
* @Override | |
*/ | |
protected function fill($row, $model) { | |
$model->cost = $row['cost']; | |
parent::fill($row, $model); | |
} | |
/** | |
* モデルの更新を行う | |
* | |
*/ | |
public function update($model) { | |
$sql = 'UPDATE leaftable_idols SET name = :name, cost = :cost WHERE id = :id'; | |
$params = []; | |
$params['name'] = $model->name; | |
$params['cost'] = $model->cost; | |
$params['id'] = $model->id; | |
return $this->execute($sql, $params); | |
} | |
} | |
/** | |
* Trainerモデル用Mapper | |
* | |
*/ | |
class TrainerMapper extends CardMapper { | |
/** | |
* 各マッパークラスに対応するモデルオブジェクトを生成する | |
*/ | |
protected function createNewInstance() { | |
return new Trainer(); | |
} | |
/** | |
* 各マッパークラスが対応するテーブル名を返す | |
*/ | |
protected function getTable() { | |
return 'leaftable_trainers'; | |
} | |
/** | |
* DBレコードから対応する値をモデルにセットする | |
* | |
* @Override | |
*/ | |
protected function fill($row, $model) { | |
$model->exp = $row['exp']; | |
parent::fill($row, $model); | |
} | |
/** | |
* モデルの更新を行う | |
* | |
*/ | |
public function update($model) { | |
$sql = 'UPDATE leaftable_trainers SET name = :name, cost = :cost, exp = :exp WHERE id = :id'; | |
$params = []; | |
$params['name'] = $model->name; | |
$params['cost'] = $model->cost; | |
$params['exp'] = $model->exp; | |
$params['id'] = $model->id; | |
return $this->execute($sql, $params); | |
} | |
} | |
/** | |
* Itemモデル用Mapper | |
* | |
*/ | |
class ItemMapper extends CardMapper { | |
/** | |
* 各マッパークラスに対応するモデルオブジェクトを生成する | |
*/ | |
protected function createNewInstance() { | |
return new Item(); | |
} | |
/** | |
* 各マッパークラスが対応するテーブル名を返す | |
*/ | |
protected function getTable() { | |
return 'leaftable_items'; | |
} | |
/** | |
* DBレコードから対応する値をモデルにセットする | |
* | |
* @Override | |
*/ | |
protected function fill($row, $model) { | |
$model->repairPoint = $row['repair_point']; | |
parent::fill($row, $model); | |
} | |
/** | |
* モデルの更新を行う | |
* | |
*/ | |
public function update($model) { | |
$sql = 'UPDATE leaftable_items SET name = :name, repair_point = :repair_point WHERE id = :id'; | |
$params = []; | |
$params['name'] = $model->name; | |
$params['repair_point'] = $model->repairPoint; | |
$params['id'] = $model->id; | |
return $this->execute($sql, $params); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment