What is a Model?

Models are PHP classes that are designed to work with information in your database. For example, let’s say you use PHPRad to manage a blog. You might have a model class that contains functions to insert, update, and retrieve your blog data. Here is an example of what such a model class might look like:

Models are optionally available for those who want to use a more traditional MVC approach.

Anatomy of a Model

Model classes are stored in your app/models/ directory.

The basic prototype for a model class is this:

class ProductsModel extends BaseModel {

	function load( $fieldname = null , $fieldvalue = null ) {
		
	}

	function view( $rec_id = null ) {
		
	}

	function add( $modeldata ) {
		
	}

	function edit( $rec_id , $modeldata=null ) {
		
	}

	function delete( $rec_ids ) {
		
	}
	
}

Where ProductsModel is the name of your class. Class names must have the first letter capitalized with the rest of the name lowercase prefixed with Model. Make sure your class extends the BaseModel class.

The file name must match the class name. For example, if this is your class:

class ProductsModel extends BaseModel {

}

Your file will be this:

app/models/ProductsModel.php

Some Basic Crude Operations Using Mysqli ORM Library By default PHPRad comes with this library

class ProductsModel extends BaseModel {

	function load( $fieldname = null , $fieldvalue = null ) {
		$fields = array(
			"product_id",
			"title",
			"description",
			"category",
			"price",
			"photo"
		);
		
		$limit = $this -> get_page_limit(); // return limit from BaseModel Class e.g array(5,20)
		
		//if search query request
		if(!empty($this -> search)){
		
			$text = $this -> search;
			$this -> db -> orWhere ( 'product_id' , "$text" , '=' );
			$this -> db -> orWhere ( 'title' , "%$text%" , 'LIKE' );
			$this -> db -> orWhere ( 'description' , "%$text%" , 'LIKE' );
			
		}
		
		//if orderby is set in the request
		if(!empty( $this -> orderby )){
			$this -> db -> orderBy($this->orderby,$this->ordertype);
		}
		
		//if record filter by table field name and value is set
		if(!empty($fieldname)){
			$this -> db -> where( $fieldname , $fieldvalue );
		}
		
		$tc = $this ->db -> withTotalCount();
		
		$arr= $this ->db -> get( 'products' , $limit, $fields );
		
		return array( "result" => $arr , "totalcount" => $tc -> totalCount );
	}

	function view( $rec_id = null ) {
	
		$fields = array(
			"product_id",
			"title",
			"description",
			"category",
			"price",
			"photo"
		);
		
		$this -> db -> where( 'product_id' , $rec_id );
		
		$obj = $this -> db -> getOne( 'products', $fields );
		
		return $obj;
	}
	
	

	function add( $modeldata ) {
		$result=array();

		$rec_id = $this -> db -> insert( 'products' , $modeldata );
		
		if( $rec_id ){
			
			$result[ 'rec_id' ] = $rec_id;
			
			$result[ 'error' ] = null;
			
		}
		else{
			$result[ 'rec_id' ] = null;
			$result[ 'error' ] = $this -> db -> getLastError();
		}
		
		return $result;
	}
	
	/**
     * edit Model Action
     * if $modeldata is empty get and return edit data else save $modeldata
     * @param $modeldata array
     * @param $rec_id value
     * @return array
     */
	function edit( $rec_id , $modeldata=null ) {
	
		if(!empty($modeldata)){
			$result = array();
			
			$this -> db -> where ( 'product_id' , $rec_id );
			
			$bool = $this-> db-> update('products',$modeldata);
			
			if( $bool ){
				
				$result['rec_id'] = $rec_id;
				$result['error'] = null;
				
			}
			else{
			
				$result['rec_id'] = null;
				$result['error'] = $this -> db -> getLastError();
				
			}
			
			return $result;
		}
		else{
		
			$fields = array(
				"product_id",
				"title",
				"description",
				"category",
				"price",
				"photo"
			);
		
			$this -> db -> where('product_id' , $rec_id);
			
			$obj = $this -> db -> getOne ('products' , $fields);
			
			return $obj;
			
		}
		
	}
	
	/**
     * delete Model Action
	 *  Support multiple record delete when records id are separated by comma (,)
     * @param $rec_id
     * @return boolean
     */
	function delete( $rec_ids ) {
	
		$arr_id = explode(",",$rec_ids);
		
		foreach($arr_id as $rec_id){
			$this->db->where('product_id' , $rec_id,"=",'OR');
		}
		$bool = $this->db->delete('products');
		
		return $bool;
		
	}
	
	
	function custom_query( $arg1 = null , $arg1 = null ) {
		
		$arr = $this -> db -> rawQuery("SELECT * FROM products WHERE status=? AND category=?", array($arg1, $ar2 ));
		
		return $arr;
	}
	
	
}