Models
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的更多相关文章
- Django models对象的select_related方法(减少查询次数)
表结构 先创建一个新的app python manage.py startapp test01 在settings.py注册一下app INSTALLED_APPS = ( 'django.contr ...
- Django models 操作高级补充
Django models 操作高级补充 字段参数补充: 外键 约束取消 ..... ORM中原生SQL写法: raw connection extra
- Django models Form model_form 关系及区别
Django models Form model_form
- Django models .all .values .values_list 几种数据查询结果的对比
Django models .all .values .values_list 几种数据查询结果的对比
- django models进行数据库增删查改
在cmd 上运行 python manage.py shell 引入models的定义 from app.models import myclass ##先打这一行 ------这些是 ...
- Django基础,Day2 - 编写urls,views,models
编写views views:作为MVC中的C,接收用户的输入,调用数据库Model层和业务逻辑Model层,处理后将处理结果渲染到V层中去. polls/views.py: from django.h ...
- 【Django】--Models 和ORM以及admin配置
Models 数据库的配置 1 django默认支持sqlite,mysql, oracle,postgresql数据库 <1>sqlite django默认使用sqlite的数据库 ...
- 广义线性模型(Generalized Linear Models)
前面的文章已经介绍了一个回归和一个分类的例子.在逻辑回归模型中我们假设: 在分类问题中我们假设: 他们都是广义线性模型中的一个例子,在理解广义线性模型之前需要先理解指数分布族. 指数分布族(The E ...
- 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' ...
- Django models知识小点
django 为使用一种新的方式,即关系对象映射(ORM) 一,创建表 1,基本结构 注意: 1,创建标的时候,如果我们不给表加自增列,生成表的时候会默认给我们生成一列为ID的自增列,当然我们也可以自 ...
随机推荐
- liunx环境下安装mysql数据库
一:如果你的机器上之前安装有mysql数据库,先进行卸载 (1)需要先将它的文件删除 (2)同时注意删除老板本的etc/my.cnf文件和/etc/mysql目录,这两个文件控制的是mysql的一些配 ...
- jquery滚动条
查看demo: 下载Demo
- SSH proxy
# for Linux ssh nobody@guoliangwu.com -P 22 -C -N -D 127.0.0.1:6500 # for windows(PuTTY) plink nobod ...
- 浅析Netty的异步事件驱动(一)
本篇文章着重于浅析一下Netty的事件处理流程,Netty版本为netty-3.6.6.Final. Netty定义了非常丰富的事件类型,代表了网络交互的各个阶段.并且当各个阶段发生时,触发相应的事件 ...
- DateTime和DateTime2
1.与ANSI和ISO8601标准的一致性不同 datetime不符合该标准,datetime2符合该标准.对于新的应用,尽量使用符合标准的类型. 2.表示范围的不同 datetime:1753-01 ...
- WEB开发总结(持续更新。。。)
近期开始搞搞web的东西,觉得有必要把遇到的问题总结一下,就在这里当做个笔记本吧. 1.用maven建立的web工程,在运行的时候,右键找不到“Run on server”菜单: 可以在命令提示行中, ...
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- HDU-4704 Sum 大数幂取模
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4704 题意:求a^n%m的结果,其中n为大数. S(1)+S(2)+...+S(N)等于2^(n-1) ...
- poj1743--Musical Theme(后缀数组)
题意:求一列数字中走向相同的两个字序列,长度要求大于5 题解:相邻数字求差,原题就变成求相同的长度大于4的子串. [存疑:在保证两个子串不相交时觉得限定条件应该是大于x,但是wa了= = 不是很理解] ...
- (二) win8+XAML Binding(数据绑定)
第一次接触Binding是用到listview的时候,ListView中如果要动态显示一些内容,我一开始想的就是动态生成一个item,然后插入... 其实用Binding就不用在代码中涉及listvi ...