Last active
March 21, 2016 04:59
-
-
Save kashiwasan/76a2e32a3b7bb673914e to your computer and use it in GitHub Desktop.
Original Model Classes
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 | |
class Member extends Model { | |
protected $tableName = 'members'; | |
} |
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 | |
/*** SQL *** | |
create database sample_db; | |
use sample_db; | |
create table members ( | |
id int auto_increment primary key, | |
name varchar(255), | |
email varchar(255), | |
created_at datetime, | |
updated_at datetime | |
); | |
*** SQLここまで ***/ | |
class Model { | |
// 外部から上書きできないように | |
protected $dbh; | |
protected $tableName; | |
protected $dsn = 'mysql:host=localhost;dbname=sample_db;charset=utf8'; | |
protected $user = 'root'; | |
protected $pass = ''; | |
public function __construct() { | |
$this->dbh = new PDO($this->dsn, $this->user, $this->pass); | |
} | |
public function execute($sql, $params = array()) { | |
$stmt = $this->dbh->prepare($sql); | |
$stmt->execute($params); | |
return $stmt; | |
} | |
public function findAll() { | |
$sql = sprintf("select * from %s", $this->tableName); | |
return $this->execute($sql)->fetchAll(PDO::FETCH_ASSOC); | |
} | |
public function findOne() { | |
$sql = sprintf("select * from %s", $this->tableName); | |
return $this->execute($sql)->fetch(PDO::FETCH_ASSOC); | |
} | |
public function findOneById($id) { | |
$sql = sprintf("select * from %s where id = :id", $this->tableName); | |
return $this->findOne($sql, array(":id" => $id)); | |
} | |
public function insert($params) { | |
$columns = array_keys($params); | |
if (!isset($params['created_at'])) | |
{ | |
$params['created_at'] = date('Y-m-d H:i:s'); | |
} | |
if (!isset($params['updated_at'])) | |
{ | |
$params['updated_at'] = date('Y-m-d H:i:s'); | |
} | |
$parameters = array(); | |
foreach ($columns as $column) | |
{ | |
if (isset($params[$column])) | |
{ | |
$parameters[':'.$column] = $params[$column]; | |
} | |
} | |
$sql = sprintf("insert into %s (%s) values (%s)", | |
$this->tableName, | |
implode(",", $columns), | |
implode(",", array_keys($parameters)) | |
); | |
$this->execute($sql, $parameters); | |
} | |
public function delete($id) { | |
if ($this->findOneById($id)) | |
{ | |
$sql = sprintf("delete from %s where id = :id", $this->tableName); | |
$this->execute($sql, array(":id" => $id)); | |
} | |
return false; | |
} | |
public function update($params) { | |
if (empty($params['id'])) | |
{ | |
return false; | |
} | |
$id = $params['id']; | |
$oldRecord = $this->findOneById($id); | |
if (!$oldRecord) { | |
return false; | |
} | |
unset($params['id']); | |
if (!isset($params['updated_at'])) | |
{ | |
$params['updated_at'] = date('Y-m-d H:i:s'); | |
} | |
$columns = array_keys($params); | |
$parameters = array(); | |
foreach ($columns as $column) | |
{ | |
if (isset($params[$column])) | |
{ | |
$parameters[':'.$column] = $params[$column]; | |
} | |
} | |
$update = array(); | |
foreach ($columns as $column) | |
{ | |
$update[] = $column.' = :'.$column; | |
} | |
// $update = array_map(function($c) { return $c.' = :'.$c; }, $columns); | |
$update = implode(', ', $update); | |
$sql = sprintf("update %s set %s where id = %d", | |
$this->tableName, | |
$update, | |
$id | |
); | |
$this->execute($sql, $parameters); | |
} | |
} | |
$member = new Member(); | |
$data_1 = array( | |
'name' => 'Kashiwagi', | |
'email' => 'kashiwagi@example.com', | |
); | |
$data_2 = array( | |
'name' => 'Ohira', | |
'email' => 'ohira@example.com', | |
); | |
$edit_data = array( | |
'id' => 2, | |
'name' => 'EDIT_NAME', | |
'email' => 'EDIT_NAME@example.com', | |
); | |
$member->insert($data_1); | |
$member->insert($data_2); | |
$record_1 = $member->findOneById(1); | |
$records = $member->findAll(); | |
$member->delete(1); | |
$member->update($edit_data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ちなみに $this->tableName の部分は、 strtolower(get_class($this)) でも代用可能なので、
そうすると、
class Member extends Model{
}
だけで、 new Member() すると、テーブル名が member となる。