PHP Class dbObject, PHP-MySQLi-Database-Class

显示文件 Open project: joshcam/PHP-MySQLi-Database-Class Class Usage Examples

Public Properties

Property Type Description
$data array An array that holds object data
$errors array An array that holds insert/update/select errors
$isNew boolean Flag to define is object is new or loaded from database
$pageLimit integer Per page limit for pagination
$returnType string Return type: 'Array' to return results as array, 'Object' as object 'Json' as json string
$totalPages integer Variable that holds total pages count of last paginate() query

Protected Properties

Property Type Description
$dbTable stating Table name for an object. Class name will be used by default
$modelPath modelPath Models path
$primaryKey stating Primary key for an object. 'id' is a default value.

Public Methods

Method Description
__call ( string $method, mixed $arg ) : mixed Catches calls to undefined methods.
__callStatic ( string $method, mixed $arg ) : mixed Catches calls to undefined static methods.
__construct ( array $data = null )
__get ( $name ) : mixed Magic getter function
__isset ( $name )
__set ( $name, $value ) : mixed Magic setter function
__toString ( ) : string Converts object data to a JSON string.
__unset ( $name )
autoload ( $path = null ) * Enable models autoload from a specified path
delete ( ) : boolean Delete method. Works only if object primaryKey is defined
insert ( ) : mixed
save ( $data = null ) : mixed Save or Update object
table ( $tableName ) : dbObject Helper function to create a virtual table class
toArray ( ) : array Converts object data to an associative array.
toJson ( ) : string Converts object data to a JSON string.
update ( array $data = null )

Protected Methods

Method Description
count ( ) : integer Function to get a total records count
get ( integer | array $limit = null, array | string $fields = null ) : array Fetch all objects
getOne ( array | string $fields = null ) : dbObject Convinient function to fetch one object. Mostly will be togeather with where()

Private Methods

Method Description
ArrayBuilder ( ) : dbObject Helper function to create dbObject with Array return type
JsonBuilder ( ) : dbObject Helper function to create dbObject with Json return type
ObjectBuilder ( ) : dbObject Helper function to create dbObject with Object return type.
byId ( $id, array | string $fields = null ) : dbObject | array Get object by primary key.
dbObjectAutoload ( $classname )
join ( string $objectName, string $key = null, string $joinType = 'LEFT', string $primaryKey = null ) : dbObject Function to join object with another object.
paginate ( integer $page, array | string $fields = null ) : array Pagination wraper to get()
prepareData ( )
processAllWith ( array &$data, $shouldReset = true ) Function queries hasMany relations if needed and also converts hasOne object names
processArrays ( array &$data )
processHasOneWith ( ) * Function building hasOne joins for get/getOne method
validate ( array $data )
with ( string $objectName ) : dbObject Function to set witch hasOne or hasMany objects should be loaded togeather with a main object

Method Details

__call() public method

Provides magic access to private functions of the class and native public mysqlidb functions
public __call ( string $method, mixed $arg ) : mixed
$method string
$arg mixed
return mixed

__callStatic() public static method

Transparently creating dbObject class to provide smooth API like name::get() name::orderBy()->get()
public static __callStatic ( string $method, mixed $arg ) : mixed
$method string
$arg mixed
return mixed

__construct() public method

public __construct ( array $data = null )
$data array Data to preload on object creation

__get() public method

Magic getter function
public __get ( $name ) : mixed
$name Variable name
return mixed

__isset() public method

public __isset ( $name )

__set() public method

Magic setter function
public __set ( $name, $value ) : mixed
return mixed

__toString() public method

Converts object data to a JSON string.
public __toString ( ) : string
return string Converted data

__unset() public method

public __unset ( $name )

autoload() public static method

Calling autoload() without path will set path to dbObjectPath/models/ directory
public static autoload ( $path = null )
$path

count() protected method

Function to get a total records count
protected count ( ) : integer
return integer

delete() public method

Delete method. Works only if object primaryKey is defined
public delete ( ) : boolean
return boolean Indicates success. 0 or 1.

get() protected method

Fetch all objects
protected get ( integer | array $limit = null, array | string $fields = null ) : array
$limit integer | array Array to define SQL limit in format Array ($count, $offset) or only $count
$fields array | string Array or coma separated list of fields to fetch
return array Array of dbObjects

getOne() protected method

Convinient function to fetch one object. Mostly will be togeather with where()
protected getOne ( array | string $fields = null ) : dbObject
$fields array | string Array or coma separated list of fields to fetch
return dbObject

insert() public method

public insert ( ) : mixed
return mixed insert id or false in case of failure

save() public method

Save or Update object
public save ( $data = null ) : mixed
return mixed insert id or false in case of failure

table() public static method

Helper function to create a virtual table class
public static table ( $tableName ) : dbObject
return dbObject

toArray() public method

Converts object data to an associative array.
public toArray ( ) : array
return array Converted data

toJson() public method

Converts object data to a JSON string.
public toJson ( ) : string
return string Converted data

update() public method

public update ( array $data = null )
$data array Optional update data to apply to the object

Property Details

$data public_oe property

An array that holds object data
public array $data
return array

$dbTable protected_oe property

Table name for an object. Class name will be used by default
protected stating $dbTable
return stating

$errors public_oe property

An array that holds insert/update/select errors
public array $errors
return array

$isNew public_oe property

Flag to define is object is new or loaded from database
public bool $isNew
return boolean

$modelPath protected_oe static_oe property

Models path
protected static modelPath $modelPath
return modelPath

$pageLimit public_oe static_oe property

Per page limit for pagination
public static int $pageLimit
return integer

$primaryKey protected_oe property

Primary key for an object. 'id' is a default value.
protected stating $primaryKey
return stating

$returnType public_oe property

Return type: 'Array' to return results as array, 'Object' as object 'Json' as json string
public string $returnType
return string

$totalPages public_oe static_oe property

Variable that holds total pages count of last paginate() query
public static int $totalPages
return integer