tp5模型笔记---一对多

来源:互联网 发布:淘宝买家怎么发优惠券 编辑:程序博客网 时间:2024/05/20 08:25
关联模型


一对一:HAS_ONE  以及对应的BELONEGS_TO
一对多:HAS_MANY 以及相对的BELONGS_TO
多对多:BELONGS_TO_MANY




步骤:


第一:创建Users模型
第二:创建Comment模型
第三:在Users模型添加方法
第四:在comment模型添加方法
第五:测试


User.phpnamespace app\index\model;use think\Model;class Users extends Model{public function comm(){return $this->hasMany('Comment','uid','user_id');}}Comment.phpnamespace app\index\model;use think\Modelclass Comment extends Model{}



在控制器中测试Index.phpnamespace app\index\Controller;use think\Controller;use think\Db;use app\index\model\Users;use app\index\model\Common;class Index extends Controller{public function test19(){$user=Users::get(1);//echo $user->nickname."<br/>";//$user->comm;foreach($user->common as $comm){echo "评论{$comm->comment_id}评论内容:{$comm->cotent}<br/>";}}public function test19r(){$user=Users::get(1);$comm=$user->comm()->where('content','这东西不错,下次还会再买')->find();echo "评论id:{$comm->comment_id}用户评论内容:{$comm->content}<br>";}public function test19w(){$user=Users::get(1);$comment=new Comment;$comment->content='ThinkPHP5视频教程';$comment->add_time=time();$user->comm()->save($comment);return "添加评论成功";//insert into `tp_comment`(`content`,`add_time`,`uid`) values(`ThinkPHP5视频教程`,1477386225,1)}public function test19ws(){$user=Users::get(1);$comment=[['content'=>'ThinkPHP5视频教程','add_time'=>time()],['content'=>'TP5视频教程','add_time'=>time()]];$user->comm()->saveAll($comment);return '添加评论成功';$user->comm()->save($comment);return "添加评论成功";}public function test19r(){//根据用户id去查询评论$user=Users::get(1,'comm');}public functioin test194(){//过滤查询结果$user=Users::get(1);//获取状态为1的关联数据  审核通过的评论$comment=$user->comm()->where('is_show',1)->select();dump($comment);//prepare select * from `tp_comment` where `uid`=? and `is_show`=?//execute select * from `tp_comment` where `uid`=1 and `is_show`=1 }public functioin test195(){//过滤查询结果$user=Users::get(1);$comm=$user->comm()->getByContent('这东西不错,11111'); echo "评论id:{$comm->comment_id}用户评论内容:{$comm->content}";}public functioin test196(){//过滤查询结果$user=Users::has('comm')->select();$user=Users::has('comm','>='2)->select();$user=Users::hasWhere('comm',['content'=>'ThinkPHP5视频教程'])->select();//select * from `tp_users` `a` join `tp_comment` `b` on `a`.`user_id`=`b`.`uid` group by b.uid having count(*) >=1//select * from `tp_users` `a` join `tp_comment` `b` on `a`.`user_id`=`b`.`uid` group by b.uid having count(*) >=2//select a.* from tp_users a join tp_comment b on a.user_id=b.uid where b.content='ThinkPHP5视频教程'}public function update(){$user=Users::get(1);$comm=$user->comm()->getByContent('ThinkPHP视频教程');$comm->content="thinkPHP5快速入门";$comm->save();//select * from `tp_user` where `user_id`=1 limit 1;//select * from `tp_comment` where `uid`=1 and `content`='ThinkPHP视频教程' LIMIT 1//update `tp_comment` set `content`='ThinkPHP5视频教程' where `comment_id`=330}public function update2(){$user=Users::get(1);$user->comm()->where('content','ThinkPHP5快速入门')->update(['content'=>'ThinkPHP5开发手册2222']);//update `tp_comment` set `content`='ThinkPHP开发手册222'where `uid`=1 and `uid`=1 and `content`='ThinkPHP快速入门';}public function del(){$user=Users::get(1);$comm=$user->comm()->getByContent('ThinkPHP5开发手册');$comm->delete();//select * from tp_users where user_id=1//select * from tp_comment where 'uid'=1 and content='ThinkPHP5开发手册' limit 1//delete from tp_comment where comment_id=330;}public function del2(){$user=Users::get(1);$user->comm()->delete();//select * from tp_users where user_id=1//delete from tp_comment where uid=1}}