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(' ...
随机推荐
- Visual Studio 2017 Add WSDL
Normal way Right click Project -> Add -> Web Reference -> Advanced Intranet way download ws ...
- vue获取不到后端返回的响应头
Response.ContentType = EPPlusHelpler.ExcelContentType; Response.Headers.Add("FileName", fi ...
- Visual Studio Code工具使用及配置
最近迷上了这个工具,启动速度快,好多插件.唯一不满意的地方就是svn版本控制工具.下面发现我装的一些插件及配置: 我安装的一些插件: 上面是我装的插件,等有时间再解释下插件的作用. 接下来说下配置: ...
- [转帖]redhat7.6Linux安装Oracle19C完整版教程
redhat7.6Linux安装Oracle19C完整版教程 https://www.oracle.com/technetwork/database/enterprise-edition/downlo ...
- linux安装 uwsgi 测试 test.py 不显示hello world 的解决办法
一般部署项目到服务器,会安装uwsgi,但是很多教程在安装它的时候会让你测试一下安装好了没,于是就有很多像我一样懵逼的少年掉进一个坑里出不来,很久.很久... 那就是最后浏览器输入ip:8000端口后 ...
- Java 阿拉伯数字转换为中文大写数字
Java 阿拉伯数字转换为中文大写数字 /** * <html> * <body> * <P> Copyright 1994 JsonInternational&l ...
- java之mybatis之字段映射及多对一
1. 数据库中表的列名和实体类的属性名称不一致. 可以使用 resultMap来解决. <select id="findAll" resultMap="UserMa ...
- C#使用Autofac实现控制反转IoC和面向切面编程AOP
Autofac是一个.net下非常优秀,性能非常好的IOC容器(.net下效率最高的容器),加上AOP简直是如虎添翼.Autofac的AOP是通过Castle(也是一个容器)项目的核心部分实现的,名为 ...
- 30个关于Shell脚本的经典案例(中)
本文目录 11.iptables自动屏蔽访问网站频繁的IP 12.判断用户输入的是否为IP地址 13.判断用户输入的是否为数字 14.给定目录找出包含关键字的文件 15.监控目录,将新创建的文件名追加 ...
- idea下java项目的打包与使用
一. 打包 (1)打开项目结构,选择Artifacts --> + --> JAR --> From modules with dependencies ... 有main方法就添加 ...