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. ubuntuy用户切换和密码修改

    修改当前用户的密码 $passwd 修改用户密码 $sudo passwd 用户名 切换到其他帐号(需要该用户的密码) $su 用户名 切换到root帐号 $sudo -s

  2. WCF 实例化与会话

    实例管理旨在解决服务实例的激活和服务实例生命周期的控制,会话的目的是在于保持相同客户端(服务代理)多次服务调用的状态. 实例上下文 实例上下文是对服务实例的封装,是WCF管理服务实例生命周期的依托,S ...

  3. codeforces 672C - Recycling Bottles 贪心水题

    感觉很简单,就是讨论一下 #include <stdio.h> #include <string.h> #include <algorithm> #include ...

  4. UVA 10600 ACM Contest and Blackout 次小生成树

    又是求次小生成树,就是求出最小生成树,然后枚举不在最小生成树上的每条边,求出包含着条边的最小生成树,然后取一个最小的 #include <iostream> #include <al ...

  5. Ofbiz初探

    转:http://xmmartin.blog.51cto.com/2310947/771236 主导建设一个电子商务系统希望从Ofbiz了解中获得一些借鉴1.下载ofbiz,目前的版本是10.04,下 ...

  6. cocos2d-x 2.x版本中,场景切换各方法调用顺序

    假设从A场景切换到B场景,调用各场景方法的顺序为: 如果没有切换效果(transition),则先调用B的init(),再调用A的onExitTransitionStart(),接着调用A的onExi ...

  7. 【noip2005】篝火晚会

    题解: 首先我们要知道一个性质: 把长度为n的序列变成目标序列最多需要n个操作 证明1: 我们可以将原序列上每位上的数字向目标序列相同位置的数字连一条有向边 如: 原序列:   1 2 3 目标序列: ...

  8. Intra Refresh of H264 encoder

    https://en.wikipedia.org/wiki/X264 x264 is able to use Periodic Intra Refresh instead of keyframes, ...

  9. UILabel的各种属性与方法的使用

    //设置字体:粗体,正常的是 SystemFontOfSize label1.font = [UIFont boldSystemFontOfSize:20]; //设置文字颜色 label1.text ...

  10. 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATI

    转载:http://my.oschina.net/u/242764/blog/375909 当我们安装好Genymotion后,把Android运用部署到上面调试时,console控制台会报错:Ins ...