abstract class \mpf\datasources\sql\DbModel

abstract class \mpf\datasources\sql\DbModel

Description of DbModel

@author mirel

Attributes
private mixed $_initiated = false

-no description found-

protected mixed $_attributes = []

-no description found-

protected mixed $_updatedAttributes = []

-no description found-

protected mixed $_originalPk

-no description found-

protected mixed $__parentRelationKey

Used internally when selecting relations.

protected string $_action = "insert"

Current action. By default is insert ( when instantiating using new Model ), few other used actions are search and update.

protected bool $_isNewRecord = true

-no description found-

protected string $_pk

Name of the column used as primary key in the table.

protected string[] $_columns = []

List of table columns from db and info about each one.

protected string $_tableName

Name of db table.

protected \mpf\datasources\sql\PDOConnection $_db

Connection to database

protected \mpf\datasources\sql\ModelInterface[] $_relations = []

List of instantiated relations.

protected array $_searchedRelations = []

List of relations that were searched and not found.

protected string[] $_errors = []

List of errors for current record

protected \mpf\tools\Validator $_validator

-no description found-

private array $_attributesForRelations = []

-no description found-

private \mpf\datasources\sql\RelationsParser $relationsParser

-no description found-

public bool $debugMode = false

Set this to true to debug relations and other info

Methods

public bool validateUnique (\mpf\datasources\sql\Validator $validator, string $field, string[] $rule, string $label, string $message)

  • $validator

    -no description found-

  • $field

    -no description found-

  • $rule

    -no description found-

  • $label

    -no description found-

  • $message

    -no description found-

!! THIS METHOD IS CALLED AUTOMATICALLY BY "Validator" object !!

Is used by Validator to check if a column is unique. In order to apply this filter for one or more values use "unique" rule in rules list. Optional parameters: 'column' or 'table'

public bool validateEnum (\mpf\datasources\sql\Validator $validator, mixed $field, mixed $rule, mixed $label, mixed $message)

  • $validator

    -no description found-

  • $field

    -no description found-

  • $rule

    -no description found-

  • $label

    -no description found-

  • $message

    -no description found-

!! THIS METHOD IS CALLED AUTOMATICALLY BY "Validator" object !!

Used for enum columns. Checks if the value is part of possible values from DB.

public bool validateExternalKey (\mpf\datasources\sql\Validator $validator, mixed $field, mixed $rule, mixed $label, mixed $message)

  • $validator

    -no description found-

  • $field

    -no description found-

  • $rule

    -no description found-

  • $label

    -no description found-

  • $message

    -no description found-

!! THIS METHOD IS CALLED AUTOMATICALLY BY "Validator" object !!

Checks if value exists in selected table for selected column.

public \mpf\tools\Validator getValidator ()

-no description found-

public null init (array $config)

  • $config

    -no description found-

It will check if it's a new record and set required attributes. Also, if it's not a new record will record originalPk to be later used for updates.

private \mpf\datasources\sql\DbModel[] getRelationFromDb (mixed $name)

  • $name

    -no description found-

Reads all relations for current model.

private array getSearchRelationsForPath (string $path)

  • $path

    -no description found-

Extracts only searched relations for specified path.

private mixed applyRelationsAttributes ()

-no description found-

public mixed relationIsSet (mixed $relationName)

  • $relationName

    -no description found-

-no description found-

public mixed __get (string $name)

  • $name

    -no description found-

-no description found-

public mixed __set (mixed $name, mixed $value)

  • $name

    -no description found-

  • $value

    -no description found-

-no description found-

public bool save (bool $validate = true)

  • $validate

    -no description found-

Save the updates or if it's a new record it will insert it.

public bool validate ()

Validate current data

public bool saveAsNew (bool $validate = true)

  • $validate

    -no description found-

It will create a copy of the current element in DB. This Object will be the new copy just inserted.

public bool delete ()

Delete current row from table.

public mixed reload ()

Reload info from DB for current object. Will also call afterLoad method in case updates are needed.

public \mpf\datasources\sql\DbModel setError (string $attribute, string $errorMessage)

  • $attribute

    -no description found-

  • $errorMessage

    -no description found-

Add error messages for an attribute

public bool hasErrors (null $attribute = null)

  • $attribute

    -no description found-

Check if the entire model has errors or a specific attribute

public string getErrors (null $attribute = null)

  • $attribute

    -no description found-

Get a list of errors for a specific attribute or for the entire model

public \mpf\datasources\sql\DbModel setAction (string $action)

  • $action

    -no description found-

Change model action.

public string getAction ()

Get current action.

public bool isNewRecord ()

Checks if current object is a new record or it's saved in DB

public mixed afterLoad ()

-no description found-

public mixed beforeSave ()

-no description found-

public mixed afterSave ()

-no description found-

public mixed beforeDelete ()

-no description found-

public mixed afterDelete ()

-no description found-

public \mpf\datasources\sql\DbModel setAttributes (string $attributes)

  • $attributes

    -no description found-

Set attributes in a safe way. it will check rules for safe attributes and it will ignore those that are not safe. To be used on search, insert, update from web form to make sure that the user doesn't add extra fields to the form.

public mixed __parentRelationKey ()

Used internally when getting relations for model.

public \mpf\datasources\sql\DbModel __invoke (mixed $pk)

  • $pk

    -no description found-

Allows quick call for PK

public static \mpf\datasources\sql\DbModel model (string $action = "search")

  • $action

    -no description found-

Get an instance of the model for selected action. Default is search.

public static int count (array $condition, array $params = [])

  • $condition

    -no description found-

  • $params

    -no description found-

Return number of models for that specific condition

public static int countByAttributes (array $attributes, null $condition = null, array $params = [])

  • $attributes

    -no description found-

  • $condition

    -no description found-

  • $params

    -no description found-

Return number of models for that specific condition

public static int deleteAll (string $condition, array $params = [])

  • $condition

    -no description found-

  • $params

    -no description found-

Delete all models that match condition.

public static int deleteAllByAttributes (array $attributes, null $condition = null, array $params = [])

  • $attributes

    -no description found-

  • $condition

    -no description found-

  • $params

    -no description found-

Delete all by attributes

public static int deleteByPk (int $pk, null $condition = null, array $params = [])

  • $pk

    -no description found-

  • $condition

    -no description found-

  • $params

    -no description found-

Delete models by primary key

public static \mpf\datasources\sql\PDOConnection getDb ()

Return connection to SQL Database

public static \mpf\datasources\sql\DbModel[] findAllByAttributes (string $attributes, null $condition = null, array $params = [])

  • $attributes

    -no description found-

  • $condition

    -no description found-

  • $params

    -no description found-

Return all models that have the selected attributes.

public static \mpf\datasources\sql\DbModel[] findAllByPk (int $pk, null $condition = null, array $params = [])

  • $pk

    -no description found-

  • $condition

    -no description found-

  • $params

    -no description found-

Return all models that have the selected primary keys.

public static null findByAttributes (array $attributes, null $condition = null, array $params = [])

  • $attributes

    -no description found-

  • $condition

    -no description found-

  • $params

    -no description found-

Return a model searched by the selected attributes and optional condition;

public static \mpf\datasources\sql\DbModel findByPk (int $pk, mixed $condition = null)

  • $pk

    -no description found-

  • $condition

    -no description found-

Return single row model;

public static \mpf\datasources\sql\DbModel find (mixed $condition, string[] $params = [])

  • $condition

    -no description found-

  • $params

    -no description found-

Return a single class for a single row;

public static \mpf\datasources\sql\DbModel[] findAll (string $condition = "1=1", array $params = [])

  • $condition

    -no description found-

  • $params

    -no description found-

Return all models that match the given condition.

public static array getRules ()

Rules examples: array( array('id, name', 'safe'), array('oldPassword, newPassword, repeatedPassword', 'safe', 'on' => 'updatePassword') )

public static array getRelations ()

Relations example: array( 'users' => array(DbRelations::BELONGSTO, '\app\models\User', 'idusr'), // type, modelClass, columnName 'lastLog' => array(DbRelations::HASONE, '\app\models\Logs', 'idobj', 'limit' => 1, 'order' => 'id DESC'), // type, modelClass, columnName ) More details on "DbRelations" class description.

public static array getLabels ()

-no description found-

public static string[] getSafeAttributes (string $action = "insert")

  • $action

    -no description found-

Get a list of safe attributes for current action or, if there are no rules then it will return an empty list.

public static array getRequiredAttributes (string $action = "insert")

  • $action

    -no description found-

Get a list of required attributes for selected action.

protected static mixed getAttributesByRule (mixed $action, mixed $rule)

  • $action

    -no description found-

  • $rule

    -no description found-

-no description found-

public static int update (mixed $pk, mixed $fields)

  • $pk

    -no description found-

  • $fields

    -no description found-

Return number of affected rows;

public static int updateAll (mixed $fields, mixed $condition, array $params = [])

  • $fields

    -no description found-

  • $condition

    -no description found-

  • $params

    -no description found-

-no description found-

public static int insert (mixed $fields, array $options = [])

  • $fields

    -no description found-

  • $options

    -no description found-

-no description found-