zoukankan      html  css  js  c++  java
  • 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 CoreModel or DatabaseModel (new Database API covered in the New Api's section).

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

    namespace AppModels;
    
    use CoreModel;
    
    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 HelpersDatabase;
    
    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 AppModelsClassname;

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

  • 相关阅读:
    recurse_array_change_key_case()递规返回字符串键名全为小写或大写的数组
    php循环创建目录
    ajaxFileUpload增加附加参数
    dedecms5.7 联动类型无法显示
    一些比较隐秘的OJ的网址
    Emacs 配置
    qwq
    233
    [八省联考2018]林克卡特树lct
    [APIO2014]序列分割
  • 原文地址:https://www.cnblogs.com/chunguang/p/5642930.html
Copyright © 2011-2022 走看看