Models
A model is a class for one database table, with the query builder, relations and links to table2 and
form2. model('film') (the same as db()->model('film')) loads the class film_model,
a yf_model, and returns a fresh copy bound to that connection.
Writing a model
// APP_PATH models/film_model.class.php
class film_model extends yf_model
{
protected $_table = 'film'; // default: the class name without _model
protected $_primary_key = 'film_id'; // default: read from the PRIMARY index
public function language()
{
return $this->belongs_to('language', 'language_id');
}
public function actors()
{
return $this->belongs_to_many('actor', 'film_actor', 'film_id', 'actor_id');
}
}
$film = model('film')->find(5); // one row, null when none
$films = model('film')->all(); // every row
$count = model('film')->count();
$new = model('film')->create(['title' => $title]); // insert, then the stored row
model('film')->destroy(5); // delete by key
$q = model('film')->where('length', '>', 100); // a query builder
Relations: has_one(), belongs_to(), has_many(), belongs_to_many(),
has_many_through(). A method scope_<name>() is a named condition; where_<column>($value)
filters by a column. table(), form($id) and filter_form() hand the model's query to
table2 and form2.
Escaping: models write through the query builder, which does not escape values unless db.ESCAPE_BUILDER_VALUES is
on (default off until yf 4.0). Escape what you pass from a request (_es(), (int)) as you would for
insert(). delete() and touch() without arguments act on the loaded row only, and
destroy() without ids does nothing.
Where models are found
The class <name>_model in a file <name>_model.class.php, looked up in this order:
APP_PATH models/: the site's own models, and overrides of the framework ones (same class name, no prefix);PROJECT_PATH share/models/;YF_PATH share/models/;- a class
<name>without the suffix that extendsyf_modeland is loaded already; - then the folders that were added on 2026-11-12:
share/models/of the app plugins and the project plugins,YF_PATH models/(a legacy folder, empty since 2026-12-03), thenshare/models/of the framework and managed plugins.
The framework's own models live in the plugin that owns their table: admin_model (sys_admin),
admin_group_model (sys_admin_groups) and user_model (user) are in
plugins/sys/share/models/. A plugin ships its models the same way, in share/models/; a managed plugin's
folder takes the place of its framework copy. Overrides keep their place: a <name>_model.class.php in
APP_PATH models/ or PROJECT_PATH share/models/ wins over every plugin.
A model that none of them has throws Exception('Not able to load model: ...'); so does a name with anything but letters,
digits and _.
$PROJECT_CONF['db']['MODEL_LOADER_FULL_PATHS'] = true; // default false, true in yf 4.0
With db.MODEL_LOADER_FULL_PATHS the folders of step 5 join the normal search order (before the framework's
share/models/, before the suffix-less class), and load('x_model') finds them too. Off, they are tried only
where model() used to throw, so every model that loaded before still loads the same class. Set it before the first
model() call of the request. Before turning it on, look for a suffix-less class x extends yf_model that a site
uses while an x_model exists in a plugin: with the flag the x_model wins.
Helpers per connection
$PROJECT_CONF['db']['OWN_HELPERS'] = true; // default false
db()->utils(), migrator() and installer() are driver classes loaded with _class().
With db.OWN_HELPERS off (as before) every connection shares one object of each, and its ->db is the
connection that asked last: a second connection (a test database, db('other'), a migration target) retargets the first
one's helper, so a later db()->utils()->table_exists() may ask the other database. On, each connection gets its own
copy. Code that keeps one connection needs nothing; code that opens a second one should turn it on, or set it on that instance
($db->OWN_HELPERS = true) before its first utils() call.