Skip to content

Instantly share code, notes, and snippets.

@phalcon
Created June 28, 2013 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phalcon/5886245 to your computer and use it in GitHub Desktop.
Save phalcon/5886245 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('records', 'test')) {
$connection->execute('CREATE TABLE IF NOT EXISTS `records` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `module` tinyint(3) unsigned NOT NULL, PRIMARY KEY (`id`), KEY `module` (`module`)) ENGINE=InnoDB AUTO_INCREMENT=1');
}
class TestController extends \Phalcon\Mvc\Controller
{
public function indexAction()
{
echo nl2br(debug_print_backtrace()), '<br>';
$record = new Records();
$record->module = 1;
$record->create();
$id = $record->id;
print_r($id);
exit();
}
}
class Records extends \Phalcon\Mvc\Model
{
public $id;
public $module;
public function getSource()
{
return 'records';
}
}
echo 'Start example <br>';
$test = new TestController();
$test->indexAction();
echo 'End example (never executed)<br>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment