Skip to content

Basic Provider Example

Steve "Uru" West edited this page Feb 24, 2015 · 2 revisions

Creating a provider

The first thing is to set up a new Provider class. This takes properties in a similar way to the v1 ORM Models. The Provider acts as a definition for the entity

use Fuel\Orm\Provider;

class PostProvider extends Provider
{
	// Specify a table name
	protected $tableName = 'posts';

	// Specify our properties
	protected $properties = [
		'id',
		'title',
		'description',
		'created_at',
		'updated_at',
	];
}

The above is the minimum configuration that is needed. Currently all primary keys are hard coded to id but this will change in the future.

Using the provider

Now that we have a provider set up we can use it to query for models. First you will need to create a database connection using https://github.com/fuelphp/database. In the future other DAL adaptors will be available but in the mean time you can implement your own version of QueryBuilderInterface if you wish.

use Fuel\Orm\QueryBuilder\Fuel as QueryBuilder;
use Fuel\Database\DB;

$connection = DB::connection(['Connection config goes in here']);
$builder = new QueryBuilder($connection);
// Now we can actually load our provider class
$provider = new PostProvider($builder);

Now we have a Provider we can start accessing entities, this is done via the Query object that you can get via $provider->getQuery(). You can use this to perform the usual create/read/update/delete actions.

$query = $provider->getQuery();

// Get a post
$post = $query->select()->where('id', '=', 1)->execute();
// Modify a post
$post->title = 'New title';
$query->reset()->update($post)->execute();
// Delete a post
$query->reset()->delete($post)->execute();
// Create and insert a new post
$newPost = $provider->forgeModelInstance(['title' => 'New Post']);
$query->reset()->insert($newPost)->execute();

Please note, this is an example of how to use the ORM in its current state. This will not be the final interface but reflects how to use the ORM package in its current state.

Clone this wiki locally