博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Models
阅读量:6425 次
发布时间:2019-06-23

本文共 1802 字,大约阅读时间需要 6 分钟。

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]].

转载地址:http://vygra.baihongyu.com/

你可能感兴趣的文章
XFire WebService开发快速起步
查看>>
JavaScript 函数replace揭秘
查看>>
QTP解决内嵌IE窗体方法2
查看>>
“王子”的演讲:N828印象
查看>>
判断JS字符串中是否包含某些字符
查看>>
Phalanger---PHP的.NET编译器
查看>>
Scanner----java控制台和文件读取的利器(java 5新增)
查看>>
如何安全设定和检测你的密码安全性?
查看>>
一例HP ADG数据恢复成功(8×73GB SCSI)
查看>>
虚拟化系列-Citrix XenServer 6.1 XenMotion与HA
查看>>
TFS创建团队项目(三)
查看>>
对发展的一点小感想
查看>>
示例化讲解RIP路由更新机制
查看>>
eclipse不能自动编译工程的解决方法
查看>>
Powershell管理系列(九)删除Exchange用户邮箱中多余的电子邮件地址
查看>>
Swt/Jface进度条
查看>>
.NET建议使用的大小写命名原则
查看>>
Git:错误:error:src refspec master does not match any
查看>>
SSIS 数据类型和类型转换
查看>>
Oracle数据库“Specified cast is农田valid”
查看>>