Skip to content
Daniel Spors edited this page Nov 11, 2020 · 5 revisions

The database abstraction layer follows a simple DB-first pattern.
It is in fact a ORM with a high level API, that allows you to query most data in a one-liner or at least create 'typed' Queries that result in Object lists.

Basic classes

There are some basic classes you found be familiar with:

The DBAL will create properties according to the table definitions found in the database.
This can be as simple as this:

$data = DataSource::Get()->Query('mytable')->like('name','%peter%');
foreach( $data as $entry )
{
	// do something
	echo "{$data->id}: {$data->name}\n";
}

In this sample the DBAL will create a ResultSet ($data) containing objects of type CommonModel, each with properties for each table column.

You will of course want to create your own classes with some business logic inside. This is done by extending the Model class:

class MyData extends Model
{
	function GetTablename(){ return 'mytable'; }

	function CanBeRemoved()
	{
		return random_int(0,100)<50;
	}
}

Note that there's no need of defining the columns. Those are added to objects of type MyData when they are queried from the database. Just go ahead and use it:

// dump all mydata assigned to account_id=1 created in the last 24 hours:
foreach( MyData::Make()->eq('account_id',1)->youngerThan('created',24,'hour') as $md )
{
	log_debug("MyData [{$md->id}]",$md->AsArray());
	if( $md->CanBeRemoved() )
		$md->Delete();
}
Clone this wiki locally