Database access
Wedeto comes with a database layer that currently supports
MySQL and PostgreSQL (we strongly recommend PostgreSQL if you
have the choice).
After the setup in the setup script, the database will be ready for you to use. In your controlleer, you can use:
$app->db
to obtain a database object. You can use this as a PDO
to prepare and execute queries. However, for most uses, the DAO class will be useful to
save you from writing the queries:
<?php
class User extends Wedeto\DB\DAO
{
protected static $table = "users";
}
$my_user = User::get(['id' => 13]);
Easy, right? Just tell Wedeto what the name of your table is, and Wedeto will find out what your table looks like and generate the queries.
If things get more complex, you can also use the Query classes to generate database agnostic queries:
<?php
use Wedeto\DB\Query\Builder as QB;
$select = QB::select(
QB::from('users'),
QB::where(
QB::any('id', [3, 5, 7, 9]),
),
QB::limit(3);
);
foreach ($select->execute($db) as $user)
{
// Do whatever you want
}
The loop will loop over the retrieved users,
$select->execute
will return a
PDOStatement
that has been prepared and executed. You just need to fetch the results.
A common scenario in a template is that you want to list all objects, and show a message when there is none. This structure is sometimes referred to as foreach-else. Wedeto implements this in a function fee:
<?php
use Wedeto\Util\Functions as WF;
WF::fee($users, function ($user) {
// Do something with the users
}, function () {
// Do something when there are no users
});