Model class implements your Business Model - single entity of your business logic.
When you plan your business application you should create classes for all your
possible business entities by extending from “Model” class.

When the code above is placed in beforeSave hook, it will only be executed
when certain fields have been changed. If your recalculations are expensive,
it’s pretty handy to rely on “dirty” fields to avoid some complex logic.

Finds a field with a corresponding name. Returns false if field not found. Similar
to hasElement() but with extra checks to make sure it’s certainly a field you are
getting.

Full example:

$m=newModel_User($db,'user');// Fields can be added after model is created$m->addField('salary',['default'=>1000]);echoisset($m['salary']);// falseecho$m['salary'];// 1000// Next we load record from $db$m->load(1);echo$m['salary'];// 2000 (from db)echoisset($m['salary']);// false, was not changed$m['salary']=3000;echo$m['salary'];// 3000 (changed)echoisset($m['salary']);// trueunset($m['salary']);// return to original valueecho$m['salary'];// 2000echoisset($m['salary']);// false$m['salary']=3000;$m->save();echo$m['salary'];// 3000 (now in db)echoisset($m['salary']);// false

This field by default is set to ‘name’ will act as a primary title field of
your table. This is especially handy if you use model inside UI framework,
which can automatically display value of your title field in the header,
or inside drop-down.

If you don’t have field ‘name’ but you want some other field to be title,
you can specify that in the property. If title_field is not needed, set it
to false or point towards a non-existent field.

Sets sorting order of returned data records. Here are some usage examples.
All these syntaxes work the same:

$m->setOrder('name, salary desc');$m->setOrder(['name','salary desc']);$m->setOrder(['name','salary'=>true]);$m->setOrder(['name'=>false,'salary'=>true]);$m->setOrder([['name'],['salary','desc']]);$m->setOrder([['name'],['salary',true]]);$m->setOrder([['name'],['salary desc']]);// and there can be many more similar combinations how to call this

Keep in mind - true means desc, desc means descending. Otherwise it will be ascending order by default.

The model is only being saved if any fields have been changed (dirty).
Sometimes it’s possible that the record in the database is no longer available
and your update() may not actually update anything. This does not normally
generate an error, however if you want to actually make sure that update() was
effective, you can implement this through a hook:

In some cases you want to prevent default actions from executing.
Suppose you want to check ‘memcache’ before actually loading the record from
the database. Here is how you can implement this functionality:

$app property is injected through your $db object and is passed around to all
the models. This hook, if successful, will prevent further execution of other
beforeLoad hooks and by specifying argument as ‘false’ it will also prevent call
to $persistence for actual loading of the data.

Similarly you can prevent deletion if you wish to implement
soft-delete or stop insert/modify from occurring.