pclib  2.6.5
Lightweight PHP framework
Public Member Functions | Static Public Member Functions | Data Fields | Protected Member Functions | Static Protected Member Functions | Protected Attributes
Model Class Reference

Base class for any application model. More...

Inheritance diagram for Model:
BaseObject

Public Member Functions

 __construct ($tableName, array $values=array())
 Create new model. More...
 
 setTemplate (Tpl $template)
 Use an existing template object. More...
 
 getTemplate ()
 Return model template. More...
 
 getColumns ()
 Return model database columns. More...
 
 getTableName ()
 Return underlying database table name.
 
 find ($id)
 Find record by primary key and load values from db. More...
 
 hasColumn ($name)
 Check if model has column $name. More...
 
 __get ($name)
 PHP magic method. More...
 
 __set ($name, $value)
 PHP magic method. More...
 
 related ($name)
 Return related model or selection of models. More...
 
 isInDb ($value=null)
 Get or set flag indicating if model has database representation. More...
 
 save ()
 Save model to the database. More...
 
 setRole ($role)
 Choose access role. More...
 
 hasRight ($action)
 Test if current role has right do $action. More...
 
 delete ()
 Delete model in database, set Model->inDb flag to false. More...
 
 onError ($action)
 Occurs on validation error.
 
 validate ($action='')
 Validate values of the model against template. More...
 
 getErrors ()
 Return array of validator errors. More...
 
 getValues ()
 Get model values. More...
 
 setValues (array $values)
 Set model values. More...
 
 toArray ()
 Get model values. More...
 
 setValue ($name, $value)
 Set column $name to $value.
 
 getValue ($name)
 Get $value of column $name.
 
 __toString ()
 PHP magic method. More...
 
- Public Member Functions inherited from BaseObject
 loadDefaults ($className=null)
 Load default parameters of class $className into object instance.
 
 setProperties (array $defaults)
 Set public properties of object from the array. More...
 

Static Public Member Functions

static create ($tableName, array $values=array(), $doSave=true)
 Create new model and store it to database. More...
 
static className ($tableName)
 Return model class name for $tableName and include model source file. More...
 
- Static Public Member Functions inherited from BaseObject
static defaults ()
 Set or retireve default parameters of the object. More...
 

Data Fields

 $db
 var Db
 
- Data Fields inherited from BaseObject
 $serviceLocator
 var function() Return service object when requested with service().
 

Protected Member Functions

 createTemplate ()
 Create template object from template file, or use default template. More...
 
 createDefaultTemplate ()
 Create default template object (empty). More...
 
 getModel ($tableName, $id=null)
 Return model for table $tableName.
 
 selection ($from=null)
 Return orm\Selection class.
 
 insert ()
 Insert new row into table with model values.
 
 update ()
 Update model values in database with actual state.
 
 validateRelated ()
 Check if rules in "event ondelete" are passed.
 
 deleteRelated ()
 Delete related models as defined in "event ondelete".
 
- Protected Member Functions inherited from BaseObject
 service ($service, $default=null)
 Try acquire $service and load it into property $this->$service. More...
 

Static Protected Member Functions

static getFilePath ($name, $ext)
 Return path to model/template file. More...
 

Protected Attributes

 $tableName
 Name of source database table.
 
 $inDb = false
 Is model stored in database?
 
 $template
 var Tpl
 
 $validator
 var Validator
 
 $values = array()
 Array of model values.
 
 $modified = array()
 Array of modified column names.
 
 $accessRole
 Role used by model for data access.
 

Detailed Description

Base class for any application model.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $tableName,
array  $values = array() 
)

Create new model.

Parameters
string$tableNameDatabase table
array$valuesModel values

Member Function Documentation

◆ __get()

__get (   $name)

PHP magic method.

Implements following features:

  • Access to column value as $model->columnName
  • Access to related model(s) as $model->relationName

Reimplemented from BaseObject.

◆ __set()

__set (   $name,
  $value 
)

PHP magic method.

Implements following features:

  • Access to column value as $model->columnName

Reimplemented from BaseObject.

◆ __toString()

__toString ( )

PHP magic method.

Return json of model values in string context.

Reimplemented from BaseObject.

◆ className()

static className (   $tableName)
static

Return model class name for $tableName and include model source file.

Parameters
string$tableNameDatabase table

◆ create()

static create (   $tableName,
array  $values = array(),
  $doSave = true 
)
static

Create new model and store it to database.

Parameters
string$tableNameDatabase table
array$valuesModel values
bool$doSaveSave to db?

◆ createDefaultTemplate()

createDefaultTemplate ( )
protected

Create default template object (empty).

Returns
Tpl $template

◆ createTemplate()

createTemplate ( )
protected

Create template object from template file, or use default template.

Returns
Tpl $template

◆ delete()

delete ( )

Delete model in database, set Model->inDb flag to false.

Returns
bool $ok

◆ find()

find (   $id)

Find record by primary key and load values from db.

Parameters
int$idprimary key
Returns
Model $this|null

◆ getColumns()

getColumns ( )

Return model database columns.

Returns
array $columns

◆ getErrors()

getErrors ( )

Return array of validator errors.

Returns
array $errors [fieldName1: errorMessage1, ...]

◆ getFilePath()

static getFilePath (   $name,
  $ext 
)
staticprotected

Return path to model/template file.

Override for your own model/template names and locations. Default location: 'models/*'

Returns
string $path

◆ getTemplate()

getTemplate ( )

Return model template.

If not exists - create one.

Returns
Tpl $template

◆ getValues()

getValues ( )

Get model values.

Returns
array $values

◆ hasColumn()

hasColumn (   $name)

Check if model has column $name.

Returns
bool $yes

◆ hasRight()

hasRight (   $action)

Test if current role has right do $action.

Parameters
string | array$action
Returns
bool $allowed

◆ isInDb()

isInDb (   $value = null)

Get or set flag indicating if model has database representation.

However, actual values can differ from database - see $this->modified.

Returns
bool $inDb

◆ related()

related (   $name)

Return related model or selection of models.

Relation $name must be defined in template elements

◆ save()

save ( )

Save model to the database.

Returns
bool $ok

◆ setRole()

setRole (   $role)

Choose access role.

Role permissions for the model can be defined in template.

Parameters
string$role

◆ setTemplate()

setTemplate ( Tpl  $template)

Use an existing template object.

Parameters
Tpl$template

◆ setValues()

setValues ( array  $values)

Set model values.

Parameters
array$values

◆ toArray()

toArray ( )

Get model values.

Returns
array $values

Reimplemented from BaseObject.

◆ validate()

validate (   $action = '')

Validate values of the model against template.

Set validation errors - see getErrors()

Parameters
string$actioninsert|update|delete
Returns
bool $isValid

The documentation for this class was generated from the following file: