Models

This class is for creating, deleting, editing and database synchronizing o "IModel" classes and synchronizing.
This class is used by the application builder to synch database with models. This class can be used for doing interesting things with the models for example you may dynamically edit/add/remove data models in an application at runtime and then synchronize the changes with the database.
Source
Models.php
Depends on
Arta , Database , ReflectionClass , ModelChunks , Model , Utils
Extends
ModelSynch

Methods

__construct

Models $obj = new Models(
    string modelsDir,
    string group,
    IDbAbstract | IDbAdvanced dbo=null,
    bool allowDrop=true,
    string tempDir=null
)

Constructor. Though the database object, temp-dir and model-directory are known to the framework, this class is app undependable, so this params are required to be passed.

Arguments

    modelsDir (string)
    Path to the model's directory
    group (string)
    Model group name
    dbo=null (IDbAbstract | IDbAdvanced)
    Database access instance, null=database actions will not be available
    allowDrop=true (bool)
    true=allow dropping database objects
    tempDir=null (string)
    Project temp directory

Returns

void

Throws

clearCache

Models $obj->clearCache(bool deleteDictionary=false)

Deletes all cache/meta files created by this class.

Arguments

    deleteDictionary=false (bool)
    false=do not delete models dictionary, it is not recommended to remove this file on a live system because each model is mapped to a number and if this dictionary is deleted the new created dictionary may have different values

Returns

Models
For method chaining

Throws

create

bool $obj->create(array def, array dataPropertyDefs, array events=null)

Create a new model class file.

Arguments

    def (array)
    Model definiition
    {
        string class     : Model class-name,
        string name      : Model class-name (alias for class),
        string file      : null
            Filename to write model into (can contain path)
            null=model-class-name.php,
        string path      : null Additional path to model-path,
        string parent    : Model Parent class,
        string creator   : Arta.Models Name of model creator,
        string updater   : Arta.Models Name of model updater,
        bool   delete    : true
            true=on delete remove data from database. false=on delete mark
            row as removed,
        string table     : model-class-name-lower-case
            Database table name to be used as model repo,
        array  private   : null Private properties {name: value,},
        array  protected : null Protected properties {name: value,},
        array  public    : null Public properties {name: value,},
        array  methods   : null {
            array  method-name : {
                bool   private   : false,
                bool   protected : false,
                string args      : null,
                string source    : null,
                string comment   : null,
            },
        },
    }
    
    dataPropertyDefs (array)
    Model data property def
    {
        property: {property-def,},
    }
    
    events=null (array)
    Events to be added
    {
        array add : {
            bool after  : true,
            bool before : true,
        },
        array update : {
            bool after  : true,
            bool before : true,
        },
        array delete : {
            bool after  : true,
            bool before : true,
        },
        array query : {
            bool after  : true,
            bool before : true,
        },
        array next : {
            bool after  : true,
        },
    }
    

Returns

bool
State of success

Throws

createDropSQLFile

bool $obj->createDropSQLFile()

Create an SQL file of "delete all model relations" SQLs.

Returns

bool
State of success

Throws

getArray

array $obj->getArray(string class, string name, string filepath=null)

Searches inside a model class PHP file for an array and returns the array value.

Arguments

    class (string)
    Model class name
    name (string)
    Target array variable name
    filepath=null (string)
    Model file path, null=use models group path

Returns

array
The found array

getModelFiles

array $obj->getModelFiles()

Get model class model class names and their file paths and also caches their last modified filetimes (to avoid synchronizing unchanged models).

Returns

array
[IModel-class-name: path-to-the-file,]

methodAdd

bool $obj->methodAdd(string class, array defs, string filepath=null)

Add a method(s) to a model file.

Arguments

    class (string)
    Model class name
    defs (array)
    Method definition
    {
        method-name: block-of-PHP-code,
    }
    
    filepath=null (string)
    Model file path, null=use models group path

Returns

bool
State of success

Throws

methodDelete

bool $obj->methodDelete(string class, string | array method, string filepath=null)

Remove method or methods from a model file. Alias of "methodRemove()".

Arguments

    class (string)
    Model class name
    method (string | array)
    Method name or [method-names,] to be removed
    filepath=null (string)
    Model file path, null=use models group path

Returns

bool
State of success

Throws

methodRemove

bool $obj->methodRemove(string class, string | array method, string filepath=null)

Remove method or methods from a model file.

Arguments

    class (string)
    Model class name
    method (string | array)
    Method name or [method-names,] to be removed
    filepath=null (string)
    Model file path, null=use models group path

Returns

bool
State of success

Throws

propertyAdd

bool $obj->propertyAdd(string class, array defs, string filepath=null)

Add/Update data property in a model file (if property exists it will be updated).

Arguments

    class (string)
    Model class name
    defs (array)
    Property definition
    filepath=null (string)
    Model file path, null=use models group path

Returns

bool
State of success

Throws

propertyDelete

bool $obj->propertyDelete(
    string class,
    string | array property,
    string filepath=null
)

Remove property or properties from a model file. Alias of "propertyRemove()".

Arguments

    class (string)
    Model class name
    property (string | array)
    Property name or [property-names,] to be removed
    filepath=null (string)
    Model file path, null=use models group path

Returns

bool
State of success

Throws

propertyRemove

bool $obj->propertyRemove(
    string class,
    string | array property,
    string filepath=null
)

Remove property or properties from a model file.

Arguments

    class (string)
    Model class name
    property (string | array)
    Property name or [property-names,] to be removed
    filepath=null (string)
    Model file path, null=use models group path

Returns

bool
State of success

Throws

remove

void $obj->remove(string class)

Remove a model file.

Arguments

    class (string)
    Model class name

Returns

void

replaceArray

bool $obj->replaceArray(
    string class,
    string name,
    array newVal,
    int indent=2,
    string filepath=null
)

Edit a model's PHP file and replace value of the first found array with newVal.

Arguments

    class (string)
    Model class name
    name (string)
    Target array variable name
    newVal (array)
    Value to be put in-place of the older one
    indent=2 (int)
    Number of indents. Each indent is four spaces
    filepath=null (string)
    Model file path, null=use models group path

Returns

bool
State of success

Throws

update

bool $obj->update(array def, array dataPropertyDefs, array events=null)

Update a model class file.

Arguments

    def (array)
    Model definiition
    {
        string class     : Model class-name,
        string name      : Model class-name (alias for class),
        string file      : null
            Filename to write model into (can contain path)
            null=model-class-name.php,
        string path      : null Additional path to model-path,
        string parent    : Model Parent class,
        string creator   : Arta.Models Name of model creator,
        string updater   : Arta.Models Name of model updater,
        bool   delete    : true
            true=on delete remove data from database. false=on delete mark
            row as removed,
        string table     : model-class-name-lower-case
            Database table name to be used as model repo,
        array  private   : null Private properties {name: value,},
        array  protected : null Protected properties {name: value,},
        array  public    : null Public properties {name: value,},
        array  methods   : null {
            array  method-name : {
                bool   private   : false,
                bool   protected : false,
                string args      : null,
                string source    : null,
                string comment   : null,
            }
        }
        array  methods-remove : null,
            List of method named to be removed from model [method1,],
    }
    
    dataPropertyDefs (array)
    Model data property def
    {
        property: {property-def,},
    }
    
    events=null (array)
    Events to be added or removed
    {
        array add : {
            bool after  : true=add false=remove,
            bool before : true=add false=remove,
        },
        array update : {
            bool after  : true=add false=remove,
            bool before : true=add false=remove,
        },
        array delete : {
            bool after  : true=add false=remove,
            bool before : true=add false=remove,
        },
        array query : {
            bool after  : true=add false=remove,
            bool before : true=add false=remove,
        },
        array next : {
            bool after  : true=add false=remove,
        },
    }
    

Returns

bool
State of success

Throws