ruckuus/php-activerecord-service-provider

A PHPActiveRecord Service Provider for Silex

Installs: 1 078

Dependents: 0

Stars: 4

Watchers: 1

Forks: 5

Language: Perl

0.0.1 2014-03-19 09:42 UTC

README

A PHP ActiveRecord ServiceProvider for Silex.

History

I was about to use Available ActiveRecord Extension, but then I realised that registerNamespace() is deprecated in later version of Silex. This work is experimental, it has a minimum functionality to "work".

Fetch

The recommended way to install ActiveRecordServiceProvider is through composer.

Just create a composer.json file for your project:

{
    "require": {
        "ruckuus/php-activerecord-service-provider": "dev-master",
        "silex/silex": "1.0.*@dev"
    }
}

Parameters

  • ar.model_dir - Path to where model folder is located ( without trailing slash )
  • ar.connections - Array of connections (name => connection). Connections examples:
    • mysql://username:password@localhost/database_name
    • pgsql://username:password@localhost/development
    • sqlite://my_database.db
    • oci://username:passsword@localhost/xe
  • ar.default_connection - default models connection.

Register

use Silex\Application;
use Ruckuus\Silex\ActiveRecordServiceProvider;

$app = new Application();

$app->register(new ActiveRecordServiceProvider(), array(
    'ar.model_dir' => __DIR__ . '/App/Model',
    'ar.connections' =>  array ('development' => 'mysql://root@localhost/database_name'),
    'ar.default_connection' => 'development',
));

Usage

Create your model in __DIR__ . '/app/Model'. Eg.

namespace App\Model;

class User extends \ActiveRecord\Model {
    static $has_many = array (
        array('problem'),
        array('luck')
    )
}

In your application:

use App\Model\User;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;

class UserProvider implements UserProviderInterface
{
    public function loadUserByUsername($username)
    {
        $user = User::find_by_username(strtolower($username));

        if ($user->dirty_attributes()) {
            throw new UnsupportedUserException(sprintf('Bad credentials for "%s"'), $username);
        }
    }
}

For more informations check the website of PHP ActiveRecord. Its wiki.