ThinkPHP模型中的HAS_ONE,BELONG_TO,HAS_MANY实践
因为很熟悉DJANGO,所以对TP,要慢慢适应。
1,SQL文件
/* Navicat MySQL Data Transfer Source Server : localhost_3306 Source Server Version : 50505 Source Host : localhost:3306 Source Database : thinkphp_inaction Target Server Type : MYSQL Target Server Version : 50505 File Encoding : 65001 Date: 2019-06-23 20:03:30 */ ; -- ---------------------------- -- Table structure for c5_post -- ---------------------------- DROP TABLE IF EXISTS `c5_post`; CREATE TABLE `c5_post` ( `post_id` ) unsigned NOT NULL AUTO_INCREMENT, `title` ) NOT NULL, `content` text NOT NULL, `created_at` ) NOT NULL, `updated_at` ) NOT NULL, `) NOT NULL, PRIMARY KEY (`post_id`) ) ENGINE DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of c5_post -- ---------------------------- '); '); -- ---------------------------- -- Table structure for c5_user -- ---------------------------- DROP TABLE IF EXISTS `c5_user`; CREATE TABLE `c5_user` ( `id` ) NOT NULL AUTO_INCREMENT, `username` ) NOT NULL, `password` ) NOT NULL, `created_at` ) NOT NULL, `updated_at` ) NOT NULL, PRIMARY KEY (`id`), KEY `username` (`username`) USING BTREE ) ENGINE DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of c5_user -- ---------------------------- '); -- ---------------------------- -- Table structure for c5_user_extra -- ---------------------------- DROP TABLE IF EXISTS `c5_user_extra`; CREATE TABLE `c5_user_extra` ( `id` ) NOT NULL AUTO_INCREMENT, `email` ) NOT NULL, `qq` ) NOT NULL, `) NOT NULL, PRIMARY KEY (`id`) ) ENGINE DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of c5_user_extra -- ---------------------------- ');
2,Application\Common\Conf\config.php
<?php
return array(
//'配置项'=>'配置值'
'DB_TYPE' => 'mysql',
'DB_HOST' => 'localhost',
'DB_PORT' => 3306,
'DB_USER' => 'root',
'DB_PWD' => 'xxxx',
'DB_NAME' => 'thinkphp_inaction',
'DB_PREFIX' => 'c5_'
);
3,Applicaton\Home\Model\UserModel.class.php
<?php
namespace Home\Model;
use Think\Model\RelationModel;
class UserModel extends RelationModel {
private $denyUserNames = array (
'admin',
'administraotr'
);
public $_validate = array(
array('username', 'require', 'user is not empty'),
array('password', 'require', 'password is not empty', 1, '', 1),
array('username', '', 'user has existed.', 0, 'unique', 1),
array('password', '6, 20', 'password length between 6~20', 0, 'length'),
array('password', '/^\w{6,20}$/', 'password format is error'),
array('password', 'repassword', 'confirm password is error', 0, 'confirm', 1),
array('username', 'checkUsername', 'user name is illegal', 0, 'callback'),
);
public $_auto = array(
array('password', 'md5', self::MODEL_BOTH, 'function'),
array('created_at', 'time', self::MODEL_INSERT, 'function'),
array('updated_at', 'time', self::MODEL_UPDATE, 'function')
);
public $_link = array(
'extra' => array(
'mapping_type' => self::HAS_ONE,
'class_name' => 'UserExtra',
'foreign_key' => 'user_id',
'mapping_fields' => 'email, qq'
),
'posts' => array(
'mapping_type' => self::HAS_MANY,
'class_name' => 'Post',
'foreign_key' => 'user_id'
)
);
public function checkUsername($username) {
foreach ($this->denyUserNames as $u) {
if (strpos($username, $u) !== false) {
return false;
}
}
return true;
}
}
?>
4,Applicaton\Home\Model\PostModel.class.php
<?php
/**
* Created by PhpStorm.
* User: Sahara
* Date: 2019/6/23
* Time: 19:00
*/
namespace Home\Model;
use Think\Model\RelationModel;
class PostModel extends RelationModel {
public $_link = array(
'author' => array(
'mapping_type' => self::BELONGS_TO,
'class_name' => 'User',
'foreign_key' => 'user_id',
)
);
}
5,Applicaton\Home\Model\PostViewModel.class.php
<?php
/**
* Created by PhpStorm.
* User: Sahara
* Date: 2019/6/23
* Time: 19:00
*/
namespace Home\Model;
use Think\Model\ViewModel;
class PostViewModel extends ViewModel {
public $viewFields = array(
'Post' => array(
'post_id',
'title',
'content',
'created_at',
'updated_at'
),
'User' => array(
'username' => 'author',
'_on' => 'Post.user_id=User.id'
)
);
}
6,Applicaton\Home\Controller\IndexController.class.php
<?php
namespace Home\Controller;
use Home\Model\PostModel;
use Home\Model\PostViewModel;
use Home\Model\UserModel;
use Think\Controller;
class IndexController extends Controller {
public function index(){
$user = new UserModel();
$data = array(
'username' => 'zhangsan',
'password' => '111111',
'repassword' => '111111'
);
if (!$user->create($data)) {
echo $user->getError();
exit;
} else {
$id = $user->add();
print_r($user->find($id));
}
echo 'ok';
}
public function posts() {
$m = new PostViewModel();
$data = $m->select();
print_r($data);
}
public function posts2() {
$m = new UserModel();
$data = $m->relation('extra')->find();
print_r($data);
}
public function posts3() {
$m = new PostModel();
$data = $m->relation('author')->find();
print_r($data);
}
public function posts4() {
$m = new UserModel();
$data = $m->relation('posts')->find();
print_r($data);
}
}

ThinkPHP模型中的HAS_ONE,BELONG_TO,HAS_MANY实践的更多相关文章
- thinkphp模型中的获取器和修改器(根据字段名自动调用模型中的方法)
thinkphp模型中的获取器和修改器(根据字段名自动调用模型中的方法) 一.总结 记得看下面 1.获取器的作用是在获取数据的字段值后自动进行处理 2.修改器的作用是可以在数据赋值的时候自动进行转换处 ...
- thinkphp在模型中自动完成session赋值
相信用过thinkphp的用户都知道thinkphp的模型可以完成很多辅助功能,比 如自动验证.自动完成等,今天在开发中遇到自动完成中需要获取session值 然后自动赋值的功能,具体看代码:clas ...
- thinkphp模型事件(钩子函数:模型中在增删改等操作前后自动执行的事件)
thinkphp模型事件(钩子函数:模型中在增删改等操作前后自动执行的事件) 一.总结 1.通过模型事件(钩子函数),可以在插入更新删除等前后执行一些特定的功能 2.模型事件是写在模型里面的,控制器中 ...
- [Asp.net MVC]Asp.net MVC5系列——从控制器访问模型中的数据
目录 概述 从控制器访问模型中的数据 强类型模型与@model关键字 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net M ...
- 错误之thinkphp模型使用发生的错误
刚接触thinkphp模型的创建,在创建model类时在这里边声明了类的对象.唉,这是不理解的错误啊.什么叫做实例化模型对象,在控制器里边使用才创建. 模型这里写各种用到的函数. 这里我也体会到了查询 ...
- thinkphp模型层Model、Logic、Service讲解
thinkphp模型层Model.Logic.Service讲解 时间:2014-08-24 15:54:56 编辑:一切随缘 文章来源:php教程网 已阅读:771 次 js特效 ...
- ThinkPHP 模型(Model)命名规范
一个小问题搞了好久:如果数据库的表名中有下划线,那么在用thinkphp做自动完成时注意Model类的命名要变成驼峰法,文件名和类名都要变.( 另外注意:只有使用create方法创建数据时才能调用到自 ...
- 浅析Thinkphp框架中运用phprpc扩展模式
浅析Thinkphp框架中应用phprpc扩展模式 这次的项目舍弃了原来使用Axis2做web服务端的 方案,改用phprpc实现,其一是服务端的thinkphp已集成有该模式接口,其二是phprpc ...
- 关联模型中如果condition条件
在练习中,有一个user表和地址表,一对多的关系. 我的想法是,通过这个关联模型找出这个用户下面默认值字段为1的地址 控制器中 public function index(){ $User = D(' ...
随机推荐
- 【tensorflow-v2.0】如何查看模型的输入输出流的属性
操作过程: 1. 查看mobilenet的variables loaded = tf.saved_model.load('mobilenet') print('MobileNet has {} tra ...
- PowerShell学习笔记
1,ps7官方文档 2,使用脚本生成帮助文档 function Add-Node { param ( $selectedNode, $name, $tag ) $newNode = new-objec ...
- linux iftop查看流量的方法
linux iftop查看流量的方法iftop 默认是查看eth0网卡的流量 这个是内网iftop -i eth1 检测eht1网卡的流量 eth1一般都是外网 具体可以iftop查看详细信息 < ...
- spring较为常用注解
@Configuration 从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被Annotat ...
- SQL Server 中的Merge关键字(转载)
简介 Merge关键字是一个神奇的DML关键字.它在SQL Server 2008被引入,它能将Insert,Update,Delete简单的并为一句.MSDN对于Merge的解释非常的短小精悍:”根 ...
- C++中STL中简单的Vector的实现
该vector只能容纳标准库中string类, 直接上代码了,StrVec.h文件内容为: #ifndef STRVEC_H #define STRVEC_H #include<iostream ...
- 【Python爬虫案例学习】Python爬取淘宝店铺和评论
安装开发需要的一些库 (1) 安装mysql 的驱动:在Windows上按win+r输入cmd打开命令行,输入命令pip install pymysql,回车即可. (2) 安装自动化测试的驱动sel ...
- Docker之Alpine制作镜像且上传至阿里云
目的: Alpine制作jdk镜像 Alpine制作jre镜像(瘦身) Docker镜像上传至阿里云 Alpine制作jdk镜像 alpine Linux简介 Alpine Linux是一个轻型Lin ...
- Golang中的RegExp正则表达式用法指南
------------------------------------------------------------ Golang中的正则表达式 ------------------------- ...
- 2019 盛趣游戏java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.盛趣游戏等公司offer,岗位是Java后端开发,因为发展原因最终选择去了盛趣游戏,入职一年时间了,也成为了面 ...