Skip to content

Instantly share code, notes, and snippets.

@ruckuus
Last active December 17, 2015 00:09
Show Gist options
  • Save ruckuus/5518215 to your computer and use it in GitHub Desktop.
Save ruckuus/5518215 to your computer and use it in GitHub Desktop.
Autoload PHPActiveRecord to work with Silex
<?php
/*
* Author: Dwi Sasongko S <[email protected]>
* See: http://www.phpactiverecord.org/projects/main/wiki/Quick_Start
*/
namespace App\Provider;
use Silex\Application;
use Silex\ServiceProviderInterface;
class ActiveRecordServiceProvider implements ServiceProviderInterface
{
private $app;
public function register(Application $app)
{
$this->app = $app;
\ActiveRecord\Config::initialize(function ($cfg) use ($app) {
$cfg->set_model_directory($app['base_dir'] . '/app/' . $app['name'] . '/Model');
$cfg->set_connections(array(
'development' => 'mysql://root@localhost/db_name',
));
$cfg->set_default_connection('development');
});
}
public function boot(Application $app)
{}
}
{
"repositories": [
{
"type": "package",
"package": {
"name": "kla/php-activerecord",
"version": "dev-master",
"autoload": {
"psr-0": {
"ActiveRecord": "lib/"
},
"files": [
"ActiveRecord.php"
]
},
"source": {
"url": "https://github.com/kla/php-activerecord",
"type": "git",
"reference": "master"
}
}
}
],
"require": {
"php-activerecord/php-activerecord": "dev-master"
}
}
<?php
namespace App\Model;
class Problem extends \ActiveRecord\Model
{
static $belongs_to = array(
array('user')
);
}
<?php
namespace App\Model;
class User extends \ActiveRecord\Model
{
static $connection = 'development';
static $attr_accessible = array('name', 'surname', 'destiny');
static $alias_attribute = array(
'alias_name' => 'name',
'alias_surname' => 'surname',
'alias_destiny' => 'destiny'
);
static $has_many = array(
array('problem'),
array('affair')
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment