ThinkPHP 学习笔记 ( 四 ) 数据库操作之关联模型 ( RelationMondel ) 和高级模型 ( AdvModel )

一、关联模型 ( RelationMondel )

1.数据查询

① HAS_ONE 查询

创建两张数据表评论表和文章表: tpk_comment , tpk_article 。评论和文章的对应关系为,一条评论 id 对应一篇文章,为 ONE_TO_ONE 关系 ( 一对一 )。评论表的结构为:

ThinkPHP 学习笔记 ( 四 ) 数据库操作之关联模型 ( RelationMondel ) 和高级模型 ( AdvModel )

其中 aid 字段与文章表的 id 字段对应。打开自定义模型 ArticleModel,让模型继承于 RelationModel,然后定义成员属性 $_link,代码:

ArticleModel.class.php:

<?php
class ArticleModel extends RelationModel{ protected $_link = array( //关联模型(表)1
'comment'=>array(
//元素(属性)定义
'mapping_type'=>HAS_ONE,
'class_name'=>"comment",
"mapping_name"=>"comment",
"foreign_key"=>"aid",
"mapping_fields"=>array("id","comment","comment_time"),
"as_fields"=>"id:comment_id,comment,comment_time",
), }

foreign_key 为必须的。

控制器:IndexAction.class.php:

<?php
class IndexAction extends Action { public function relation(){ $obj=D("Article");
$rows=$obj->field('id,title')->relation(true)->select();
dump($rows);
}
}

浏览器输出:

array(6) {
[0] => array(5) {
["id"] => string(1) "1"
["title"] => string(4) "test"
["comment_id"] => string(1) "1"
["comment"] => string(15) "第一条评论"
["comment_time"] => string(19) "2014-11-22 22:40:01"
}
[1] => array(5) {
["id"] => string(1) "2"
["title"] => string(12) "吼吼吼吼"
["comment_id"] => string(1) "2"
["comment"] => string(15) "第二条评论"
["comment_time"] => string(19) "2014-11-22 22:40:06"
}
[2] => array(5) {
["id"] => string(1) "4"
["title"] => string(7) "test2_m"
["comment_id"] => NULL
["comment"] => NULL
["comment_time"] => NULL
}
[3] => array(5) {
["id"] => string(1) "5"
["title"] => string(2) "22"
["comment_id"] => NULL
["comment"] => NULL
["comment_time"] => NULL
}
[4] => array(5) {
["id"] => string(1) "6"
["title"] => string(1) "1"
["comment_id"] => NULL
["comment"] => NULL
["comment_time"] => NULL
}
[5] => array(5) {
["id"] => string(1) "7"
["title"] => string(6) "lalala"
["comment_id"] => NULL
["comment"] => NULL
["comment_time"] => NULL
}
}

http://127.0.0.26/index.php/index/relation

此时文章表和评论表的数据为:

评论表:

ThinkPHP 学习笔记 ( 四 ) 数据库操作之关联模型 ( RelationMondel ) 和高级模型 ( AdvModel )

文章表:

ThinkPHP 学习笔记 ( 四 ) 数据库操作之关联模型 ( RelationMondel ) 和高级模型 ( AdvModel )

② BELONGS_TO 查询

一条评论数据对应一个用户 id,评论表和用户表之间的关系是 BELONGS_TO

创建自定义模型 CommentModel,继承于RelationModel:

CommentModel.class.php:

<?php
class CommentModel extends RelationModel{ protected $_link = array( 'user'=>array( "mapping_type"=>BELONGS_TO,
"foreign_key"=>"comment_user",
"mapping_fields"=>array("id","username"),
"as_fields"=>"id:user_id,username", ), );
}

tpk_user 表结构

ThinkPHP 学习笔记 ( 四 ) 数据库操作之关联模型 ( RelationMondel ) 和高级模型 ( AdvModel )

数据:

ThinkPHP 学习笔记 ( 四 ) 数据库操作之关联模型 ( RelationMondel ) 和高级模型 ( AdvModel )

控制器 ArticleModel.class.php:

<?php
class IndexAction extends Action { //BELONGS_TO
public function relation2(){ $obj=D("Comment");
$rows=$obj->relation(true)->select();
dump($rows);
} }

访问 http://127.0.0.26/index.php/index/relation2

显示:

array(2) {
[0] => array(7) {
["id"] => string(1) "1"
["aid"] => string(1) "1"
["comment"] => string(15) "第一条评论"
["comment_time"] => string(19) "2014-11-22 22:40:01"
["comment_user"] => string(1) "1"
["user_id"] => string(1) "1"
["username"] => string(3) "dee"
}
[1] => array(7) {
["id"] => string(1) "2"
["aid"] => string(1) "2"
["comment"] => string(15) "第二条评论"
["comment_time"] => string(19) "2014-11-22 22:40:06"
["comment_user"] => string(1) "2"
["user_id"] => string(1) "2"
["username"] => string(4) "yoko"
}
}

③ HAS_MANY

一篇文章可以对应多条评论,评论表和文章表之间的关系为 HAS_MANY

此时评论表的数据为:

ThinkPHP 学习笔记 ( 四 ) 数据库操作之关联模型 ( RelationMondel ) 和高级模型 ( AdvModel )

模型:CommentModel.class.php:

<?php
class CommentModel extends RelationModel{ //关联模型2 HAS_MANY
'comment'=>array( 'mapping_type'=>HAS_MANY,
'foreign_key'=>"aid",
), }

控制器 ArticleModel.class.php:

<?php
class IndexAction extends Action { //HAS_MANY
public function relation3(){ $obj=D("Article");
$rows=$obj->relation(true)->select();
dump($rows);
}
}

访问 http://127.0.0.26/index.php/index/relation3

显示:

array(6) {
[0] => array(8) {
["id"] => string(1) "1"
["title"] => string(4) "test"
["content"] => string(12) "test_content"
["category"] => string(13) "test_category"
["area"] => string(6) "北京"
["add_user"] => string(5) "admin"
["add_time"] => string(19) "2014-11-20 23:03:44"
["comment"] => array(2) {
[0] => array(5) {
["id"] => string(1) "1"
["aid"] => string(1) "1"
["comment"] => string(15) "第一条评论"
["comment_time"] => string(19) "2014-11-22 22:40:01"
["comment_user"] => string(1) "1"
}
[1] => array(5) {
["id"] => string(1) "2"
["aid"] => string(1) "1"
["comment"] => string(15) "第二条评论"
["comment_time"] => string(19) "2014-11-22 22:40:06"
["comment_user"] => string(1) "2"
}
}
}
[1] => array(8) {
["id"] => string(1) "2"
["title"] => string(12) "吼吼吼吼"
["content"] => string(18) "任溶溶柔然人"
["category"] => string(14) "test_category2"
["area"] => string(6) "河北"
["add_user"] => string(5) "admin"
["add_time"] => string(19) "2014-11-22 15:16:12"
["comment"] => NULL
}
[2] => array(8) {
["id"] => string(1) "4"
["title"] => string(7) "test2_m"
["content"] => string(4) "haha"
["category"] => string(0) ""
["area"] => string(6) "福建"
["add_user"] => NULL
["add_time"] => string(19) "2014-11-22 11:44:26"
["comment"] => NULL
}
[3] => array(8) {
["id"] => string(1) "5"
["title"] => string(2) "22"
["content"] => NULL
["category"] => string(0) ""
["area"] => string(6) "福建"
["add_user"] => NULL
["add_time"] => string(19) "2014-11-22 12:40:58"
["comment"] => NULL
}
[4] => array(8) {
["id"] => string(1) "6"
["title"] => string(1) "1"
["content"] => string(1) "2"
["category"] => string(0) ""
["area"] => string(6) "福建"
["add_user"] => NULL
["add_time"] => NULL
["comment"] => NULL
}
[5] => array(8) {
["id"] => string(1) "7"
["title"] => string(6) "lalala"
["content"] => string(6) "hohoho"
["category"] => string(0) ""
["area"] => string(6) "北京"
["add_user"] => NULL
["add_time"] => NULL
["comment"] => NULL
}
}

2.数据操作

以 HAS_MANY 为例,在tpk_article中添加一篇文章,同时 tpk_comment 中添加两条评论。代码:

控制器 ArticleModel.class.php:

    //HAS_MANY 操作,tpk_article中添加一篇文章,同时tpk_comment中添加两条评论
public function reladd(){ $obj=D("Article");
$data['title']="新闻标题";
$data['content']='这是一条体育新闻';
$data['add_time']=date("Y-m-d H:i:s",time());
//关联的模型
$data['comment']=array(
array("aid"=>20,"comment"=>"体育新闻评论1","comment_time"=>date("Y-m-d H:i:s",time()),"comment_user"=>1),
array("aid"=>20,"comment"=>"体育新闻评论2","comment_time"=>date("Y-m-d H:i:s",time()),"comment_user"=>1),
);
$obj->relation(true)->add($data);
}

另一个例子,删除文章的同时也删除了评论:

控制器 IndexAction.class.php:

<?php
class IndexAction extends Action { //HAS_MANY 删除
public function reldel(){ $obj=D("Article");
$obj->relation(true)->delete(20);
echo $obj->getLastSql();
} }

二、高级模型 ( AdvModel )

高级模型除了可以实现普通模型所有功能之外,还能够实现数据过滤、操作限制、延迟操作等功能。

例子:

定位查询 getN() , first , last

getN(2) :获取结果集正序第 3 条记录

Model 代码:

<?php
class ArticleModel extends AdvModel{ //....
}

控制器代码:

<?php

class IndexAction extends Action {

        //使用高级模型定位查询
// $obj=D("Article");
// $rows=$obj->getN(2);
// dump($rows);
// echo $obj->getLastSql(); //使用高级模型动态查询
// $obj=D("Article");
// $rows=$obj->getByArea("北京");
// dump($rows);
// echo $obj->getLastSql(); //使用高级模型动态查询2
// $obj=D("Article");
// $rows=$obj->getFieldByArea("北京","id");
// dump($rows);
// echo $obj->getLastSql(); //使用高级模型动态查询3
// $obj=D("Article");
// $rows=$obj->top5();
// dump($rows);
// echo $obj->getLastSql();
}
}

参考资料:《PHP MVC 开发实战》

上一篇:自己对WSO2 ESB 见解


下一篇:Windows安装Python环境和Python集成开发环境(IDE)PyCharm