GithubHelp home page GithubHelp logo

wordpressdev / my_model Goto Github PK

View Code? Open in Web Editor NEW

This project forked from carternerds/my_model

0.0 2.0 0.0 2.07 MB

A My_Model class to provide CRUD functionality for other Model cllasses to inherit actions.

License: Other

my_model's Introduction

A My_Model class to provide CRUD functionality for other Model classes to inherit actions.

Extending Class
**************************************************************************************
A model class would extend the My_Model class in the following way

class Posts extends MY_Model{
    
    function __construct()
    {
        parent::__construct();

        /***************************************
        Set the model info
        ***************************************/
        
        //Table name
        $this->table_name = "posts";

        //Primary Key
        $this->primary_key = "post_id";

        //Default order by
        $this->order_by = "post_id";

        //Default sort direction
        $this->sort = "desc";
        
        //Required fields
        $this->required_fields = array(
            "post_title",
            "post_body",
            "post_pubdate",
            "post_author",
            "post_slug"
        );

    }

}


======================================================================================


Get
**************************************************************************************
Intended to provide the majority of get requests
**************************************************************************************

//Get all entries
$this->posts->get();

//Get a single entry by it's id
$this->posts->get(1);

//Get multiple entries
$options['post_id'] = array(1,2);
$this->posts->get($options);

//Get all values sorted in DESC order
$this->posts->sort = "desc";
$this->posts->get();

//Select all entries return a specific field
$this->posts->get("post_id");


//Select all entries return multiple fields
$options['select'] = array("post_title", "post_id");
$this->posts->get($options);

//Select all with a condition
$options['where'] = array("post_title" => "This is my first post");
$this->posts->get($options);

//Select all with a comparison condition
$options['where'] = array("post_pubdate <" => '2011-12-18');
$this->posts->get($options);

//Select all like (and like)
$options['like'] = array("post_author" => "Some", "post_body" => "Lorem");
$this->posts->get($options);

//Select all like (or like)
$options['or_like'] = array("post_author" => "Some", "post_body" => "Lorem");
$this->posts->get($options);   

//Get all and return as a key value array
$options['key_value'] = array("post_title", "post_author");
$this->posts->get($options);

//Select all with a limit and offset
$this->posts->limit = 10;
$this->posts->offset = 20;
$this->posts->get();


======================================================================================



Save
**************************************************************************************
Intended to provide the majority of insert and update requests

Adding required fields to the post array will validate 
-their presence and that they have a value

//Required fields example
$this->required_fields = array(
    "post_title",
    "post_body",
    "post_pubdate",
    "post_author",
    "post_slug"
);

When saving an entry fields will be checked to ensure they exist.

**************************************************************************************

//Save a single entry
$data["post_title"] = "Post title";
$data["post_body"] = "Hello dolor sit amet, consectetur adipiscing elit.";
$data["post_pubdate"] = "2012-01-27 16:57:48";
$data["post_author"] ="Paul Beardsell";
$data["post_slug"] = "post-title";
$insert_id = $this->posts->save($data);

//Save a multiple entries
$data[0]["post_title"] = "Post title";
$data[0]["post_body"] = "Hello dolor sit amet, consectetur adipiscing elit.";
$data[0]["post_pubdate"] = "2012-01-27 16:57:48";
$data[0]["post_author"] ="Paul Beardsell";
$data[0]["post_slug"] = "post-title";
$data[1]["post_title"] = "Post title 2";
$data[1]["post_body"] = "Hello dolor sit amet, consectetur adipiscing elit.";
$data[1]["post_pubdate"] = "2012-01-27 16:57:48";
$data[1]["post_author"] ="Paul Beardsell";
$data[1]["post_slug"] = "post-title-2";
$insert_id_array = $this->posts->save($data);

//Update an entry
$data["post_title"] = "This is the post title";
$data["post_slug"] = "this-is-the-post-title";
$this->posts->save($data, 1);


======================================================================================


Delete
**************************************************************************************
Intended to provide the majority of delete requests
**************************************************************************************

//Delete a single entry
$this->posts->delete(1);

//Delete a multiple entries
$this->posts->delete(array(1,2));


/////////////////////////////////////////////////////////////////////////////////////

Inspiration from:

Jamie Rumbelow
@jamierumbelow 

Joost van Veen
@joostvanveen 

/////////////////////////////////////////////////////////////////////////////////////

my_model's People

Contributors

carternerds avatar

Watchers

James Cloos avatar Rafique avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.