简单的activeRecord实现
时间:2008-08-29
来源:互联网
小弟初学php,在phpchina潜水一段时间受益良多, 现在把自制的activeRecord实现奉上, 有很多拙劣的地方, 希望前辈门能够点评一二
post.model.php
这段代码中把所有的数据存储工作都交给了__get和__set, 这就很糟糕地导致每次sql都只修改一个数据,产生一个页面可能会有上百条sql产生,就像这样...囧:
select title from __posts where id=1
select content from __posts where id=1
select created_at from __posts where id=1
select update_at from __posts where id=1
select title from __posts where id=2
select content from __posts where id=2
select created_at from __posts where id=2
select update_at from __posts where id=2
......
[php]
<?php
## file post.model.php
## <Post> has many <comments>
class Post{
private $id=0;
private static $fields = array('title','content','created_at','updated_at');
##static
#create a new post object , factory method ?
#=returns <Post> a post object whick created recently
static function create($data=array()){
mysql_query('insert into __posts values ();');
$result=mysql_query('select id from __posts order by id desc limit 1'); #get the new id
$post=Post::find(mysql_result($result,0,'id'));
foreach ($data as $key=>$value){
$post->$key=$value;
}
return $post;
}
#find a post
#=returns <Post> a post object
static function find($id){
return new Post($id);
}
#list posts by page number
#=returns <Array> , an array of <Post>
static function list_in_page($page_no,$page_size=10){
$result=mysql_query("select id from __posts order by created_at desc limit ".(--$page_no)*$page_size.",".$page_size.";");
$arr=array();
while($row = mysql_fetch_assoc($result)){
$arr[] = self::find($row['id']);
}
return $arr;
}
#list posts which created recently
#=returns <Array> ,an array of <Post>
static function list_recent($size=20){
return self::list_in_page(0,$size);
}
#list posts
#=returns <Array> ,an array of <Post>
static function list_all(){
$result=mysql_query("select id from __posts order by created_at desc;");
$arr=array();
while($row = mysql_fetch_assoc($result)){
$arr[] = self::find($row['id']);
}
return $arr;
}
#returns the total number of all posts
#=returns <Value> , a number
static function count_all(){
$result=mysql_query("select count(*) as count from __posts");
return mysql_result($result,0,'count');
}
##private
#private constructor
private function Post($id){
$this->id=$id;
}
##public
#remove this post
function remove(){
mysql_query("delete from __posts where id='".$this->id."';");
mysql_query("delete from __comments where post_id='".$this->id."';");
}
#setter, set values of this field
function __set($name,$value){
if (!in_array($name,self::$fields)) throw new Exception('unkown field');
mysql_query("update __posts set ".$name." = '".$value."' where id='".$this->id."' ;");
}
#getter, get the values of this field
#=returns <value>
function __get($name){
if ($name=='id')
return $this->id;
else{
$result=mysql_query("select ".$name." from __posts where id='".$this->id."';");
return mysql_result($result,0,$name);
}
}
#Post has many comments
#=returns <array> an array of Comment object
function get_comments(){
$result=mysql_query("select id from __comments where post_id='".$this->id."' order by created_at");
$arr=array();
while($row=mysql_fetch_assoc($result)){
$arr[]=Comment::find($row['id']);
}
return $arr;
}
#add a comment to this <Post>
function add_comment($comment){
$comment->post_id=$this->id;
}
}
?>
[/php]
comments.model.php
<?php
## file comment.model.php
## <Comments> belongs to <Post>
class Comment{
private $id=0;
private static $fields=array('post_id','name','content','created_at');
##static
#create a new comment object, factory method?
#=returns <Comment> , a newly created object
static function create($data=array()){
mysql_query('insert into __comments values ();');
$result=mysql_query('select id from __comments order by id desc limit 1'); #get the new id
$comment=self::find(mysql_result($result,0,'id'));
foreach ($data as $key=>$value){
$comment->$key=$value;
}
return $comment;
}
#find a comment object by id
#=returns <Comment>
static function find($id){
return new Comment($id);
}
##private
#private constructor
private function Comment($id){
$this->id=$id;
}
##public
#setter, set values of this field
public function __set($name,$value){
if (!in_array($name,self::$fields)) throw new Exception('unkown field');
mysql_query("update __comments set ".$name." = '".$value."' where id='".$this->id."' ;");
}
#getter, set values of this field
#=returns <Value>
public function __get($name){
if (!in_array($name,self::$fields)) throw new Exception('unkown field');
if ($name=='id')
return $this->id;
else{
$result=mysql_query("select $name from __comments where id='".$this->id."' limit 1;");
return mysql_result($result,0,$name);
}
}
#remove this comment *but how to destroy this object manually?
public function remove(){
mysql_query("delete from __comments where id='".$this->id."';");
}
}
?>
post.model.php
这段代码中把所有的数据存储工作都交给了__get和__set, 这就很糟糕地导致每次sql都只修改一个数据,产生一个页面可能会有上百条sql产生,就像这样...囧:
select title from __posts where id=1
select content from __posts where id=1
select created_at from __posts where id=1
select update_at from __posts where id=1
select title from __posts where id=2
select content from __posts where id=2
select created_at from __posts where id=2
select update_at from __posts where id=2
......
[php]
<?php
## file post.model.php
## <Post> has many <comments>
class Post{
private $id=0;
private static $fields = array('title','content','created_at','updated_at');
##static
#create a new post object , factory method ?
#=returns <Post> a post object whick created recently
static function create($data=array()){
mysql_query('insert into __posts values ();');
$result=mysql_query('select id from __posts order by id desc limit 1'); #get the new id
$post=Post::find(mysql_result($result,0,'id'));
foreach ($data as $key=>$value){
$post->$key=$value;
}
return $post;
}
#find a post
#=returns <Post> a post object
static function find($id){
return new Post($id);
}
#list posts by page number
#=returns <Array> , an array of <Post>
static function list_in_page($page_no,$page_size=10){
$result=mysql_query("select id from __posts order by created_at desc limit ".(--$page_no)*$page_size.",".$page_size.";");
$arr=array();
while($row = mysql_fetch_assoc($result)){
$arr[] = self::find($row['id']);
}
return $arr;
}
#list posts which created recently
#=returns <Array> ,an array of <Post>
static function list_recent($size=20){
return self::list_in_page(0,$size);
}
#list posts
#=returns <Array> ,an array of <Post>
static function list_all(){
$result=mysql_query("select id from __posts order by created_at desc;");
$arr=array();
while($row = mysql_fetch_assoc($result)){
$arr[] = self::find($row['id']);
}
return $arr;
}
#returns the total number of all posts
#=returns <Value> , a number
static function count_all(){
$result=mysql_query("select count(*) as count from __posts");
return mysql_result($result,0,'count');
}
##private
#private constructor
private function Post($id){
$this->id=$id;
}
##public
#remove this post
function remove(){
mysql_query("delete from __posts where id='".$this->id."';");
mysql_query("delete from __comments where post_id='".$this->id."';");
}
#setter, set values of this field
function __set($name,$value){
if (!in_array($name,self::$fields)) throw new Exception('unkown field');
mysql_query("update __posts set ".$name." = '".$value."' where id='".$this->id."' ;");
}
#getter, get the values of this field
#=returns <value>
function __get($name){
if ($name=='id')
return $this->id;
else{
$result=mysql_query("select ".$name." from __posts where id='".$this->id."';");
return mysql_result($result,0,$name);
}
}
#Post has many comments
#=returns <array> an array of Comment object
function get_comments(){
$result=mysql_query("select id from __comments where post_id='".$this->id."' order by created_at");
$arr=array();
while($row=mysql_fetch_assoc($result)){
$arr[]=Comment::find($row['id']);
}
return $arr;
}
#add a comment to this <Post>
function add_comment($comment){
$comment->post_id=$this->id;
}
}
?>
[/php]
comments.model.php
<?php
## file comment.model.php
## <Comments> belongs to <Post>
class Comment{
private $id=0;
private static $fields=array('post_id','name','content','created_at');
##static
#create a new comment object, factory method?
#=returns <Comment> , a newly created object
static function create($data=array()){
mysql_query('insert into __comments values ();');
$result=mysql_query('select id from __comments order by id desc limit 1'); #get the new id
$comment=self::find(mysql_result($result,0,'id'));
foreach ($data as $key=>$value){
$comment->$key=$value;
}
return $comment;
}
#find a comment object by id
#=returns <Comment>
static function find($id){
return new Comment($id);
}
##private
#private constructor
private function Comment($id){
$this->id=$id;
}
##public
#setter, set values of this field
public function __set($name,$value){
if (!in_array($name,self::$fields)) throw new Exception('unkown field');
mysql_query("update __comments set ".$name." = '".$value."' where id='".$this->id."' ;");
}
#getter, set values of this field
#=returns <Value>
public function __get($name){
if (!in_array($name,self::$fields)) throw new Exception('unkown field');
if ($name=='id')
return $this->id;
else{
$result=mysql_query("select $name from __comments where id='".$this->id."' limit 1;");
return mysql_result($result,0,$name);
}
}
#remove this comment *but how to destroy this object manually?
public function remove(){
mysql_query("delete from __comments where id='".$this->id."';");
}
}
?>
作者: ssword 发布时间: 2008-08-29
加几个使用实例吧。。。呵呵
作者: kakashilw 发布时间: 2008-08-29
:-)
添加post
$post=Post::create(array(
'title'=>$title,
'content'=>$content
));
给post添加评论
$post->add_comment(
Comment::create(array(
'name'=>$name,
'content'=>$content
))
);
修改数据(突然想起来貌似忘了写update, 囧):
$post=Post::find($id)
$post->title='test';
$post->content='testtest';
删除post
$Post::find($id)->remove();
枚举post的comments
foreach ($Post::find($id)->get_comments as $c){
echo '<p>'.$c->name.'</p>'
echo '<p>'.$c->content.'</p>'
echo '<p>'.$c->created_at.'</p>'
}
添加post
$post=Post::create(array(
'title'=>$title,
'content'=>$content
));
给post添加评论
$post->add_comment(
Comment::create(array(
'name'=>$name,
'content'=>$content
))
);
修改数据(突然想起来貌似忘了写update, 囧):
$post=Post::find($id)
$post->title='test';
$post->content='testtest';
删除post
$Post::find($id)->remove();
枚举post的comments
foreach ($Post::find($id)->get_comments as $c){
echo '<p>'.$c->name.'</p>'
echo '<p>'.$c->content.'</p>'
echo '<p>'.$c->created_at.'</p>'
}
作者: ssword 发布时间: 2008-08-29
徐希了
作者: fireseno 发布时间: 2008-12-01
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28