pclib  2.3.2
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. More...
 
 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...
 
 addEvent ($name, $callback)
 Add event handler to $object->$name property. More...
 
 toArray ()
 Convert object to 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
 $onNewInstance
 Occurs when new instance of the class is created. More...
 
 $onMissingMethod
 Occurs when called method does not exist. More...
 
 $serviceLocator
 var function() Return service object when requested with service(). More...
 

Protected Member Functions

 createTemplate ()
 Create template object from template file, or use default template. More...
 
 createDefaultTemplate ()
 Create default template object (empty). More...
 
 insert ()
 Insert new row into table with model values. More...
 
 update ()
 Update model values in database with actual state. More...
 
 validateRelated ()
 Check if rules in "event ondelete" are passed.
 
 deleteRelated ()
 Delete related models as defined in "event ondelete".
 
- Protected Member Functions inherited from BaseObject
 fireEvent ($name, array $args=array())
 Run all event handlers in $object->$name property. More...
 
 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. More...
 
 $inDb = false
 Is model stored in database?
 
 $template
 var Tpl
 
 $validator
 var Validator
 
 $values = array()
 Array of model values. More...
 
 $modified = array()
 Array of modified column names. More...
 
 $accessRole
 Role used by model for data access. More...
 

Detailed Description

Base class for any application model.

Constructor & Destructor Documentation

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

Create new model.

Parameters
string$tableNameDatabase table
array$valuesModel values

Member Function Documentation

__get (   $name)

PHP magic method.

Implements following features:

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

PHP magic method.

Implements following features:

  • Access to column value as $model->columnName
__toString ( )

PHP magic method.

Return json of model values in string context.

static className (   $tableName)
static

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

Parameters
string$tableNameDatabase table
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 ( )
protected

Create default template object (empty).

Returns
Tpl $template
createTemplate ( )
protected

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

Returns
Tpl $template
delete ( )

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

Returns
bool $ok
find (   $id)

Find record by primary key and load values from db.

Parameters
int$idprimary key
Returns
Model $this|null
getColumns ( )

Return model database columns.

Returns
array $columns
getErrors ( )

Return array of validator errors.

Returns
array $errors [fieldName1: errorMessage1, ...]
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 ( )

Return model template.

If not exists - create one.

Returns
Tpl $template
getValues ( )

Get model values.

Returns
array $values
hasColumn (   $name)

Check if model has column $name.

Returns
bool $yes
hasRight (   $action)

Test if current role has right do $action.

Parameters
string | array$action
Returns
bool $allowed
insert ( )
protected

Insert new row into table with model values.

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
onError (   $action)

Occurs on validation error.

related (   $name)

Return related model or selection of models.

Relation $name must be defined in template elements

save ( )

Save model to the database.

Returns
bool $ok
setRole (   $role)

Choose access role.

Role permissions for the model can be defined in template.

Parameters
string$role
setTemplate ( Tpl  $template)

Use an existing template object.

Parameters
Tpl$template
setValues ( array  $values)

Set model values.

Parameters
array$values
toArray ( )

Get model values.

Returns
array $values
update ( )
protected

Update model values in database with actual state.

validate (   $action = '')

Validate values of the model against template.

Set validation errors - see getErrors()

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

Field Documentation

$accessRole
protected

Role used by model for data access.

$modified = array()
protected

Array of modified column names.

$tableName
protected

Name of source database table.

$values = array()
protected

Array of model values.


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