Skip to content

Instantly share code, notes, and snippets.

@phalcon
Created May 20, 2013 04:21
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save phalcon/5610396 to your computer and use it in GitHub Desktop.
<?php
use Phalcon\DI,
Phalcon\Events\Manager as EventsManager,
Phalcon\Db\Adapter\Pdo\Mysql as Connection,
Phalcon\Mvc\Model\Manager as ModelsManager,
Phalcon\Mvc\Model\Metadata\Memory as ModelsMetaData;
$eventsManager = new EventsManager();
$di = new DI();
$connection = new Connection(array(
"host" => "localhost",
"username" => "root",
"password" => "",
"dbname" => "test"
));
$connection->setEventsManager($eventsManager);
$eventsManager->attach('db',
function ($event, $connection) {
switch ($event->getType()) {
case 'beforeQuery':
echo $connection->getSqlStatement(), "<br>\n";
break;
}
}
);
$di['db'] = $connection;
$di['modelsManager'] = new ModelsManager();
$di['modelsMetadata'] = new ModelsMetadata();
if (!$connection->tableExists('user')) {
$connection->execute('CREATE TABLE user (id integer primary key auto_increment, email varchar(120) not null)');
}
class User extends \Phalcon\Mvc\Model
{
public $id;
public $email;
public static function myCustomUserCreator()
{
$newUser = new User();
$newUser->email = 'test';
if ($newUser->save() == false) {
return false;
} else {
return $newUser->id;
}
}
}
print User::myCustomUserCreator();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment