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(' ...
随机推荐
- Spring Boot启动时出现WARN:No MyBatis mapper was found in
今天发现spring-boot继承mybatis启动时老是出现WARN: org.mybatis.spring.mapper.ClassPathMapperScanner - No MyBatis m ...
- spket插件安装并设置JQuery自动提示
下载地址: com.spket.ui_1.6.23jar包license替换破解.zip: https://pan.baidu.com/s/1JooqlkhHczPT0V34-Qm0oA 提取码: s ...
- LeetCode176——第二高的薪水
题目描述 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+--------+ | 1 ...
- Linux内核参数详解
所谓Linux服务器内核参数优化(适合Apache.Nginx.Squid等多种web应用,特殊的业务有可能需要做略微调整),主要是指在Linux系统中针对业务服务应用而进行的系统内核参数调整,优化并 ...
- PHP变量回收
PHP变量回收1 离开页面2 unset
- 推荐系统中的协同滤波算法___使用SVD
对于推荐方法,基于内容 和 基于协同过滤 是目前的主流推荐算法,很多电子商务网站的推荐系统都是基于这两种算法的. 协同过滤 是一种基于相似性来进行推荐的算法,主要分为 基于用户的协同过滤算法 和 基于 ...
- 接口和抽象类的区别,注意JDK8的接口可以有实现。
Java中,抽象类和接口有相似的地方.下面我们就来细说说接口和抽象类的异同. 首先是相同的地方: 1. 接口和抽象类都能定义方法和属性. 2. 接口和抽象类都是看作是一种特殊的类.大部分的时候,定义的 ...
- 如何改变 select 元素的高度
mozilla 对于美化 select 元素的样式有这样一段描述(用 CSS 美化 Select 元素): 众所周知,select 元素很难用 CSS 进行高效的设计.你可以影响任何元素的某些方面 - ...
- stvd使用中的一些问题
1.stm8_interrupt_vector.c 会莫名其妙的自动出现,而且都是在项目目录下.进行如下操作 2.stvd编译时遇到no default placement for segment . ...
- go标准库I/O模型:epoll+多协程
本文为linux环境下的总结,其他操作系统本质差别不大.本地文件I/O和网络I/O逻辑类似. epoll+多线程的模型 epoll+多线程模型和epoll 单进程区别.优点 对比于redis这 ...