Wiki
Clone wikiAnano 2 / Database
Database and ORM
Anano has a built-in ORM and query builder for simple database operations. For many more advanced queries, such as condition grouping in where clauses, the answer to "how do I do that" is simply "you can't", or to switch to the more feature complete but somewhat heavier PHP ActiveRecord, for which support is also built in.
ActiveRecord
To use ActiveRecord instead of the built-in ORM, simply add it to your composer.json.
#!json { "require": { "php-activerecord/php-activerecord": "dev-master" } }
Then run composer install
from your terminal of choice. Remember to set your models to extend ActiveRecord\Model
. Anano will configure ActiveRecord for you.
Other DBALs and ORMs can of course be installed as well, but you will need to configure them yourself.
Native
Out of the box you can extend your models from Anano\ORM\Model
to use the built-in ORM.
Note: All tables must have a unique id column to use them with the ORM, though you can override the name of the column by setting public $id_column
in the model.
Note: Models by default bind themselves to a table with the same name as the model, plus an 's' at the end. That means a model named User
will bind to the table users
. This can be overridden by setting public $table_name
in the model.
You can now manipulate the table as a PHP object. For example, to print and change the username of the user with id 1:
#!php $model = new User(1); echo $model->username; $model->username = 'Testuser'; $model->save();
Note: The save()
method will only do anything if one or more fields have been changed in the model. You can force it to save, e.g. to update timestamps, by passing true
as the first parameter (force).
Query builder
For anything more complicated than manipulating a single identified row, you can use the query builder.
A contrived example just as an introduction:
#!php $model = new User; $results = $model ->where('username', '<>', 'Testuser') ->orderBy('username', 'asc') ->get();
Modifiers
select($arg1, [$arg2, ...])
Takes a variable number of fields to retrieve. You can use periods and aliasing with as
as in normal SQL. If omitted, selects all (*). Usually unnecessary.
from($table_name)
or table($table_name)
Choose table. If you follow table naming conventions or have set $table_name
in your model, this is unnecessary.
where($column, $comparison, $variable)
Conditions, e.g. where('id', '=', 1)->where('name', 'LIKE', '%b%')
. Use orWhere()
for OR conditions. Currently no support for grouping.
join($table, $var1, $comparison, $var2, [$direction])
Also leftJoin
, rightJoin
and innerJoin
.
groupBy($column1, [$column2, ...])
A variable number of fields to group by, in given order.
orderBy($column, [$direction])
Order by chosen column. Direction defaults to "ASC".
limit($number)
Max number of rows to fetch.
offset($number)
Rows to skip before fetching. Limit applies after.
Actions
get()
Executes the built query and returns found rows as relevant model objects or null
if none.
first()
Returns first row out of the found collection. Equivalent of get()[0]
result()
Executes built query and returns found rows as a native PHP array.
update($fields)
Updates the table with an associative array of columns and values.
insert($fields)
As update but inserts.
replace($fields)
As insert but replaces.
Updated