Models

Models control the data source, they are used for collecting and issuing data, this could be a remote service, as XML, JSON or using a database to get and fetch records.

A Model is a class. The model needs to extend the parent Model, either the Core\Model or Database\Model (new Database API covered in the New Api's section).

The model should have a namespace of App\Models when located in the root of the app/Models directory.

namespace App\Models;

use Core\Model;

class Contacts extends Model
{ }

The parent model is very simple, it's the only role is to create an instance of the database class located in (system/Helpers/Database.php) once set the instance is available to all child models that extend the parent model.

namespace Core;

use Helpers\Database;

class Model
{
protected $db; public function __construct()
{
//connect to PDO here.
$this->db = Database::get(); }
}

Models can be placed in the root of the models folder. The namespace used in the model should reflect its file path. Classes directly in the models folder will have a namespace of models or if in a folder: namespace App\Models\Classname;

Methods inside a model are used for getting data and returning data back to the controller, a method should never echo data only return it, it's the controller that decides what is done with the data once it's returned.

The most common use of a model is for performing database actions, here is a quick example:

public function getContacts()
{
return $this->db->select('SELECT firstName, lastName FROM '.PREFIX.'contacts');
}

This is a very simple database query $this->db is available from the parent model inside the $db class holds methods for selecting, inserting, updating and deleting records from a MySQL database using PDO more on this topic in the section [[Database]].

Models的更多相关文章

  1. Django models对象的select_related方法(减少查询次数)

    表结构 先创建一个新的app python manage.py startapp test01 在settings.py注册一下app INSTALLED_APPS = ( 'django.contr ...

  2. Django models 操作高级补充

    Django models 操作高级补充 字段参数补充: 外键 约束取消 ..... ORM中原生SQL写法: raw connection extra

  3. Django models Form model_form 关系及区别

    Django models Form model_form

  4. Django models .all .values .values_list 几种数据查询结果的对比

    Django models .all .values .values_list 几种数据查询结果的对比

  5. django models进行数据库增删查改

    在cmd 上运行 python manage.py shell   引入models的定义 from app.models import  myclass   ##先打这一行    ------这些是 ...

  6. Django基础,Day2 - 编写urls,views,models

    编写views views:作为MVC中的C,接收用户的输入,调用数据库Model层和业务逻辑Model层,处理后将处理结果渲染到V层中去. polls/views.py: from django.h ...

  7. 【Django】--Models 和ORM以及admin配置

    Models 数据库的配置 1    django默认支持sqlite,mysql, oracle,postgresql数据库 <1>sqlite django默认使用sqlite的数据库 ...

  8. 广义线性模型(Generalized Linear Models)

    前面的文章已经介绍了一个回归和一个分类的例子.在逻辑回归模型中我们假设: 在分类问题中我们假设: 他们都是广义线性模型中的一个例子,在理解广义线性模型之前需要先理解指数分布族. 指数分布族(The E ...

  9. Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet

    在程序中要添加django.setup() 整个程序如下所示 import os import django def populate(): python_cat = add_cat('Python' ...

  10. Django models知识小点

    django 为使用一种新的方式,即关系对象映射(ORM) 一,创建表 1,基本结构 注意: 1,创建标的时候,如果我们不给表加自增列,生成表的时候会默认给我们生成一列为ID的自增列,当然我们也可以自 ...

随机推荐

  1. (2015年郑州轻工业学院ACM校赛题) J 堆

    判断是否是一个堆,把树构造好遍历一遍就OK了 #include<stdio.h> #include<iostream> #include<stack> #inclu ...

  2. 缓存你的BITMAP对象

    在app中通常最占内存.占流量的元素就是图片了,图片往往又无处不在,特别是伴随着list,GridView或者ViewPager出现,这些图片随着你的滑动操作,时而出现在你的屏幕中,时而消失在屏幕之外 ...

  3. 仿酷狗音乐播放器开发日志三——修复CEditUI的bug2

    无意中发现了CEditUI控件的另一个bug,当我给播放器的搜索栏获取焦点时,这时再改变窗体大小,原本搜索栏应该对应着也改变大小,却发现CEditUI内嵌的edit控件没有跟着改变(如下图),跟着调试 ...

  4. 《Oracle Database 12c DBA指南》第二章 - 安装Oracle和创建数据库(2.2 安装数据库软件)

    当前关于12c的中文资料比较少,本人将关于DBA的一部分官方文档翻译为中文,很多地方为了帮助中国网友看懂文章,没有按照原文句式翻译,翻译不足之处难免,望多多指正. 2.2 安装数据库软件 这部分简短讲 ...

  5. H5制作1--母亲节快乐

    H5作品现在很流行额. 早上起的很早.就自己试着去了解了一下.感觉很easy. 用了百度的H5在线制作工具.感觉很easy.顺便给百度做下推广吧.h5.baidu.com 于是就有了自己的第一个dem ...

  6. uva 11916 Emoogle Grid

    题意:用K种颜色给一个N*M的格子涂色.其中有B个格子是不能涂色的.涂色时满足同一列上下紧邻的两个格子的颜色不同.所有的涂色方案模100000007后为R.现在给出M.K.B.R,求一个最小的N,满足 ...

  7. 《GettingThingsDone》--GTD学习笔记(一)-GTD理论

    利用春节假期阅读了<Getting Things Done>一书,下文整理了下阅读过程中做的读书笔记和心得. ==GTD理论== 一. 目的: 1. 收集需要处理的事情把它置于一个脱离大脑 ...

  8. 题解西电OJ (Problem 1008 - 数星星)

    题目内容: Description “不要问我太阳有多高 我会告诉你我有多真 不要问我星星有几颗 我会告诉你很多很多” 一天Qinz和wudired在天上数星星,由于星星可以排列成一条直线,他们比赛看 ...

  9. leetcode@ [54/59] Spiral Matrix & Spiral Matrix II

    https://leetcode.com/problems/spiral-matrix/ Given a matrix of m x n elements (m rows, n columns), r ...

  10. Java注解处理器使用详解

    在这篇文章中,我将阐述怎样写一个注解处理器(Annotation Processor).在这篇教程中,首先,我将向您解释什么是注解器,你可以利用这个强大的工具做什么以及不能做什么:然后,我将一步一步实 ...