Skip to content

CarterNerds/My_Model

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 

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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors