dev-master
9999999-dev https://github.com/superjimpupcake/PupcakePHPActiveRecordA plugin component for Pupcake Framework to use phpactiverecord library
MIT
The Requires
- php >=5.3.0
database micro framework lightweight activerecord microframework
Wallogit.com
2017 © Pedro Peláez
A plugin component for Pupcake Framework to use phpactiverecord library
Pupcake PHPActiveRecord is a plugin component for Pupcake Framework to work with the PHP ActiveRecord library (http://www.phpactiverecord.org/), (*1)
Before we get started, we would like to set up the following file structures for our web application:, (*2)
We first need to check out php activerecord library from github: https://github.com/kla/php-activerecord.git, (*3)
git clone https://github.com/kla/php-activerecord.git packages/php-activerecord, (*4)
Now we are going to write classes/Model/User.php, (*5)
<?php
namespace Model;
use ActiveRecord;
class User extends ActiveRecord\Model
{
}
The User model wraps around the users table., (*6)
Now we are going to write our public/index.php, (*7)
<?php
//Assuming this is public/index.php and the composer vendor directory is ../vendor
require_once __DIR__.'/../vendor/autoload.php';
$loader = new Composer\Autoload\ClassLoader();
$loader->add("Model", "../classes");
$loader->register();
$app = new Pupcake\Pupcake();
$app->usePlugin("Pupcake.PHPActiveRecord", array(
'phpactiverecord_loader' => __DIR__."/../packages/php-activerecord/ActiveRecord.php", // this is the phpactiverecord's main loader file
'configuration_callback' => function($cfg)
{
$cfg->set_connections(array('development' => 'mysql://root:@localhost/phpactiverecords'));
}, //this is the configuration callback for phpactiverecord
));
$app->get("user/create", function($req, $res){
try{
$user = new Model\User(array('name' => md5(time()), 'email' => time().'@dummy.com'));
$user->save();
$res->send("User is created successfully!");
}
catch(Exception $e){
$res->send("Fail adding user!");
}
});
$app->run();
In public/index.php, we use composer's class loader to register namespace Model so that we can use Model/User class We then specify the phpactiverecord's main loader file location and the configuration callback function. Then in the user/create route, we create a random user record., (*8)
We must make sure that out local mysql database has the phpactiverecords database setup and with a users table which has the following fields: * id * name * email, (*9)
A plugin component for Pupcake Framework to use phpactiverecord library
MIT
database micro framework lightweight activerecord microframework