-
Notifications
You must be signed in to change notification settings - Fork 1
AQL Models
Start by defining your $sky_aql_model_path
in config.php, you can start using the model system.
Assuming $sky_aql_model_path = 'models/';
In the models
directory, you need a folder with the model name, we'll use 'artist.' In the artist folder, should be:
artist {
name,
bio
}
class artist extends model {
}
- _aql - stores actual .aql file when it is found in the models directory or the input aql
- _aql_array - array generated by _aql
- _data - all stored data, corresponds to properties and what they represent
-
_do_set - can be set prior to
loadDB
, will make sure object's data does not pull from cache - _errors - errors array
-
_id - the identifier, (if set) during
loadDB
- _model_name - set by the class name
- _objects - an array of objects (also in the properties array, also as keys), distinguishing between plural and singular objects
- _primary_table - the primary table of the model, to which the _id applies
-
_properties - an array of properties (as keys in the array) can be fetched with
model::getProperties
- _required_fields - an array of required fields to be checked before save, in 'field_name' => 'Field' pairs
-
_return - information to add to the return array (
after_save
,after_fail
)
- addProperty
- after_fail
- after_save
- before_save
- construct
- dataToArray
- delete
- failTransaction
- get
- loadArray
- loadDB
- loadJSON
- preValidate
- reload
- returnJSON
- requiredField
- removeProperty
- save
- validate
class artist extends model {
public function construct() {
$this->addProperty('free_show', 'animals', 'coding', 'abc', 'def');
}
}
$o = new artist;
$o->free_show = 'free';
$o->animals = 1;
(string) Property name to add (string) (optional) You can add many properties this way.
(model) So you can chain methods.
You cannot save the properties to the DB, this just makes it easier to interact with information.
###This function is called when you use the save method and the transaction fails.
(array) The save_array will be passed to the function
(array) The standard response array for model::save
(array) The save_array
(array) The standard response array for model::save
model::before_save
is executed once the save transaction starts.
You can change save_array in this function or trigger transaction failure.
You must return the save array
(array) The save_array
(array) The save_array
This is a function that executes automatically after the __construct
method of a model.
class artist extends model {
public function construct() {
if ($this->_id) { // if the object is loaded
$this->addProperty('fans')->loadFans();
print_pre($this->fans);
}
}
public function loadFans() {
// some fan loading logic here
$this->fans = aql::select($some_query);
}
}
none
(null)
The function returns a nested array of the object data, which is stored in $o->_data
, since this can have sub objects, as defined by the model.
Deletes the current object's record (sets active = 0
) and returns the response array (status, data, errors)
Triggers a database transaction failure (useful for testing), should be called in model::before_save
Returns a model object, it checks to see if the class for the specified model exists and returns that, or returns a generic model object. The difference being the generic model object would have none of the specific methods that could be added/overwritten by making an artist class.
$o = model::get('artist', $id);
// equivalent to
// $o = new artist($id);
// or
// $o = new model($id, 'artist');
// if class artist does not exist
(array) Parses an array based on the AQL used to define it and populates _data
array.
(model) so you can chain methods.
$o = new artist();
$response = $o->loadArray($array)->save();
- (string) identifier, the ID or IDE of the record in the database.
- (bool) defaults false. If this is set to true, it will not fetch the object from cache (if cache is enabled), but will use
aql::profile
and populate the cache
(model) so you can chain methods.
$o = new artist();
$response = $o->loadDB(10)->delete();
Decodes JSON to an array and uses loadArray();
(string) JSON string.
(model)
Used after save to refresh the object's data with the current information in the database. This will set the cache.
A hook that can be defined in any object class that will run before model::validate()
returns a JSON string
- (array)
(string)
Usage is the same as addProperty
used during validation, if the $val
is empty, an error is added to the _errors
array.
- (string) Name of field, to be used in the error if the value is empty
- (string) Value of field
(bool) true if value exists
class artist extends model {
set_name($val) {
return $this->requiredField('Artist Name', $val);
}
}
$o = new artist();
$o->loadArray(array(
'name' => $name,
'bio' => $bio
));
$response = $o->save();
(array)
If the save fails, results of the function model::after_fail
are returned, otherwise, model::after_save
Both of these can be overridden in the artist
class but are already defined.
array(
'status' => $status,
'data' => $data,
'errors' => $errors
)
Where $status
is either OK
or Error
.
If $status != 'OK'
, it is expected that $errors = $this->_errors
,
else $data = $this->_data
or $data = $this->dataToArray()
Is automatically run during model::save()
.
It looks for methods that look like set_field_name($val)
after checking _required_fields array for field_name.