ROR을 사용하시는 분들께 권장해드립니다.
Model이라고 말하긴 그렇고
그냥 Restful하게 쿼리를 사용하는 방법입니다.
코드 출처는 제가 만든거고,
예전 제가 만든 MVC모델에서 Model부분만 떼가지고 온 겁니다.
작업 1. /lib/common.lib.php 맨 뒤에 이코드를 붙여주세요.
class model {
var $tablename = null;
var $primary_key = null;
function __construct($tablename, $primary_key) {
$this->primary_key = $primary_key;
$this->tablename = $tablename;
}
function find($options) {
if(@$options['first'])
$limit = " LIMIT 1";
else if(@$options['all'])
$limit = "";
if(@$options['select'])
$select = $options['select'];
else
$select = "*";
if(@$options['conditions'])
$where = "WHERE ".$options['conditions'];
if(@$options['joins'])
$joins = $options['joins'];
if(@$options['order'])
$order = "ORDER BY $options[order]";
if(@$options['group'])
$group = "GROUP BY $options[group]";
if(@$options['limit'])
$limit = "LIMIT $options[limit]";
$query = @sprintf("SELECT %s FROM `%s` %s %s %s %s %s", $select
, $this->tablename
, $joins
, $where
, $group
, $order
, $limit);
$res = sql_query($query);
while($row = mysql_fetch_assoc($res))
$result[] = $row;
if(count($result) > 0 ) {
if(@$options['all']) {
foreach($result AS $idx => $object) {
$result_object[$idx] = new SimpleResult($this->primary_key, $this->tablename);
$result_object[$idx]->SettingVariable($object);
}
} else if($options['first']) {
$result_object = new SimpleResult($this->primary_key, $this->tablename);
$result_object->SettingVariable($result[0]);
}
} else {
$result_object = null;
}
return $result_object;
}
function create() {
return new SimpleResult($this->primary_key, $this->tablename);
}
};
class SimpleResult {
protected $old_variable = null;
public $primary_key = null;
protected $table = null;
public $db = null;
public $database = "default";
function SimpleResult($primary_key, $table) {
$this->primary_key = $primary_key;
$this->table = $table;
}
function SettingVariable($variable) {
$this->old_variable = $variable;
foreach($variable AS $key => $value)
$this->$key = $value;
return;
}
function save() {
$array = array();
$primary_key = $this->primary_key;
foreach(get_object_vars($this) AS $key => $value) {
if($key == "old_variable" || $key == "primary_key" || $key == "db" || $key == "table" || $key == "database")
continue;
else {
if(count($this->old_variable) > 0) {
if($this->old_variable[$key] != $this->$key) {
if(@strlen($change) == 0)
$change = sprintf("`%s` = '%s'", $key, $this->$key);
else
$change .= sprintf(", `%s` = '%s'", $key, $this->$key);
}
// $array = array_merge($array, array($value));
} else {
if(@strlen($insert_column) == 0) {
$insert_column = "`$key`";
$insert_value = sprintf("'%s'", $this->$key);
} else {
$insert_column .= ", `$key`";
$insert_value .= sprintf(", '%s'", $this->$key);
}
}
}
}
if(@strlen($change) > 0) {
$primary_key = $this->primary_key;
$query = sprintf("UPDATE %s SET %s WHERE %s = '%s'", $this->table
, $change
, $primary_key
, $this->old_variable[$primary_key]);
} else if(strlen($insert_column) > 0) {
$query = sprintf("INSERT INTO %s (%s) VALUES (%s)", $this->table
, $insert_column
, $insert_value);
} else {
return;
}
sql_query($query);
return;
}
function delete() {
$query = sprintf("DELETE FROM `%s`
WHERE `%s` = '%s'", $this->table,
$this->primary_key,
$this->old_variable[$this->primary_key]);
sql_query($query);
return;
}
function send($key) {
return $this->$key;
}
function classObject() {
return $this->old_variable;
}
}
작업 2. 사용방법
::: Model 호출 방법
$model = new model(tablename, primary_key);
이렇게 호출해야 합니다. (그누보드는 MVC모델로 제작된 것이 아니어서 primarykey를 명시해줘야 합니다.)
g4_write_bobo테이블 사용하기(Restful한 table명이 아니므로 자동으로 s가 붙지 않습니다.)
ex) $model = new model("g4_write_bobo", "wr_id");
::: Insert 쿼리 날리기
$model = new model("g4_write_bobo", "wr_id");
$create = $model->create();
$create->wr_name = "테스트";
$create->title = "제목입니다.";
$create->save();
이것은
INSERT INTO g4_write_bobo (`wr_name`, `title`) VALUES ('테스트', '제목입니다.'); 와 같은 쿼리입니다
::: SELECT쿼리 날리기
게시물에 title 중 '바보'가 포함된 게시글 가져와서 출력하기
$model = new model("g4_write_bobo", "wr_id");
$result = $model->find(array("all" => true, "conditions" => "title = '바보'"));
( = SELECT * FROM g4_write_bobo WHERE title = '바보')
foreach($result AS $res) {
echo $res->title;
}
::: UPDATE쿼리 날리기
게시물의 게시물 번호 3번의 타이틀을 '천재'로 변경하기
$model = new model("g4_write_bobo", "wr_id");
$result = $model->find(array("first" => true, "select" => "wr_id", "conditions" => "wr_id = 3"));
( = SELECT wr_id FROM g4_write_bobo WHERE wr_id = 3 LIMIT 1)
$result->title = "천재";
$result->save();
( = UPDATE g4_write_bobo SET title = '천재' WHERE wr_id = 3)
이상 궁금한 점은 코멘트로 물어봐주시면 답변해드리겠습니다.
버그 발견하시면 저에게 알려주세요!
Model이라고 말하긴 그렇고
그냥 Restful하게 쿼리를 사용하는 방법입니다.
코드 출처는 제가 만든거고,
예전 제가 만든 MVC모델에서 Model부분만 떼가지고 온 겁니다.
작업 1. /lib/common.lib.php 맨 뒤에 이코드를 붙여주세요.
class model {
var $tablename = null;
var $primary_key = null;
function __construct($tablename, $primary_key) {
$this->primary_key = $primary_key;
$this->tablename = $tablename;
}
function find($options) {
if(@$options['first'])
$limit = " LIMIT 1";
else if(@$options['all'])
$limit = "";
if(@$options['select'])
$select = $options['select'];
else
$select = "*";
if(@$options['conditions'])
$where = "WHERE ".$options['conditions'];
if(@$options['joins'])
$joins = $options['joins'];
if(@$options['order'])
$order = "ORDER BY $options[order]";
if(@$options['group'])
$group = "GROUP BY $options[group]";
if(@$options['limit'])
$limit = "LIMIT $options[limit]";
$query = @sprintf("SELECT %s FROM `%s` %s %s %s %s %s", $select
, $this->tablename
, $joins
, $where
, $group
, $order
, $limit);
$res = sql_query($query);
while($row = mysql_fetch_assoc($res))
$result[] = $row;
if(count($result) > 0 ) {
if(@$options['all']) {
foreach($result AS $idx => $object) {
$result_object[$idx] = new SimpleResult($this->primary_key, $this->tablename);
$result_object[$idx]->SettingVariable($object);
}
} else if($options['first']) {
$result_object = new SimpleResult($this->primary_key, $this->tablename);
$result_object->SettingVariable($result[0]);
}
} else {
$result_object = null;
}
return $result_object;
}
function create() {
return new SimpleResult($this->primary_key, $this->tablename);
}
};
class SimpleResult {
protected $old_variable = null;
public $primary_key = null;
protected $table = null;
public $db = null;
public $database = "default";
function SimpleResult($primary_key, $table) {
$this->primary_key = $primary_key;
$this->table = $table;
}
function SettingVariable($variable) {
$this->old_variable = $variable;
foreach($variable AS $key => $value)
$this->$key = $value;
return;
}
function save() {
$array = array();
$primary_key = $this->primary_key;
foreach(get_object_vars($this) AS $key => $value) {
if($key == "old_variable" || $key == "primary_key" || $key == "db" || $key == "table" || $key == "database")
continue;
else {
if(count($this->old_variable) > 0) {
if($this->old_variable[$key] != $this->$key) {
if(@strlen($change) == 0)
$change = sprintf("`%s` = '%s'", $key, $this->$key);
else
$change .= sprintf(", `%s` = '%s'", $key, $this->$key);
}
// $array = array_merge($array, array($value));
} else {
if(@strlen($insert_column) == 0) {
$insert_column = "`$key`";
$insert_value = sprintf("'%s'", $this->$key);
} else {
$insert_column .= ", `$key`";
$insert_value .= sprintf(", '%s'", $this->$key);
}
}
}
}
if(@strlen($change) > 0) {
$primary_key = $this->primary_key;
$query = sprintf("UPDATE %s SET %s WHERE %s = '%s'", $this->table
, $change
, $primary_key
, $this->old_variable[$primary_key]);
} else if(strlen($insert_column) > 0) {
$query = sprintf("INSERT INTO %s (%s) VALUES (%s)", $this->table
, $insert_column
, $insert_value);
} else {
return;
}
sql_query($query);
return;
}
function delete() {
$query = sprintf("DELETE FROM `%s`
WHERE `%s` = '%s'", $this->table,
$this->primary_key,
$this->old_variable[$this->primary_key]);
sql_query($query);
return;
}
function send($key) {
return $this->$key;
}
function classObject() {
return $this->old_variable;
}
}
작업 2. 사용방법
::: Model 호출 방법
$model = new model(tablename, primary_key);
이렇게 호출해야 합니다. (그누보드는 MVC모델로 제작된 것이 아니어서 primarykey를 명시해줘야 합니다.)
g4_write_bobo테이블 사용하기(Restful한 table명이 아니므로 자동으로 s가 붙지 않습니다.)
ex) $model = new model("g4_write_bobo", "wr_id");
::: Insert 쿼리 날리기
$model = new model("g4_write_bobo", "wr_id");
$create = $model->create();
$create->wr_name = "테스트";
$create->title = "제목입니다.";
$create->save();
이것은
INSERT INTO g4_write_bobo (`wr_name`, `title`) VALUES ('테스트', '제목입니다.'); 와 같은 쿼리입니다
::: SELECT쿼리 날리기
게시물에 title 중 '바보'가 포함된 게시글 가져와서 출력하기
$model = new model("g4_write_bobo", "wr_id");
$result = $model->find(array("all" => true, "conditions" => "title = '바보'"));
( = SELECT * FROM g4_write_bobo WHERE title = '바보')
foreach($result AS $res) {
echo $res->title;
}
::: UPDATE쿼리 날리기
게시물의 게시물 번호 3번의 타이틀을 '천재'로 변경하기
$model = new model("g4_write_bobo", "wr_id");
$result = $model->find(array("first" => true, "select" => "wr_id", "conditions" => "wr_id = 3"));
( = SELECT wr_id FROM g4_write_bobo WHERE wr_id = 3 LIMIT 1)
$result->title = "천재";
$result->save();
( = UPDATE g4_write_bobo SET title = '천재' WHERE wr_id = 3)
이상 궁금한 점은 코멘트로 물어봐주시면 답변해드리겠습니다.
버그 발견하시면 저에게 알려주세요!
댓글 2개
게시글 목록
| 번호 | 제목 |
|---|---|
| 34706 | |
| 34693 | |
| 34563 | |
| 34536 | |
| 34521 | |
| 34480 | |
| 34479 | |
| 34466 | |
| 34437 | |
| 34436 | |
| 34435 | |
| 34406 | |
| 34398 | |
| 34387 | |
| 34382 | |
| 34375 | |
| 34364 | |
| 34336 | |
| 34294 | |
| 34293 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기