Created
May 6, 2013 18:20
-
-
Save phalcon/5527000 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Phalcon\DI, | |
Phalcon\Events\Manager as EventsManager, | |
Phalcon\Db\Adapter\Pdo\Sqlite 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( | |
"dbname" => "robots.db" | |
)); | |
$connection->setEventsManager($eventsManager); | |
$eventsManager->attach('db', | |
function ($event, $connection) { | |
switch ($event->getType()) { | |
case 'beforeQuery': | |
echo $connection->getSqlStatement(), '<br>'; | |
break; | |
case 'beginTransaction': | |
echo 'BEGIN', '<br>'; | |
break; | |
case 'commitTransaction': | |
echo 'COMMIT', '<br>'; | |
break; | |
case 'rollbackTransaction': | |
echo 'ROLLBACK', '<br>'; | |
break; | |
} | |
} | |
); | |
$di['db'] = $connection; | |
$di['modelsManager'] = new ModelsManager(); | |
$di['modelsMetadata'] = new ModelsMetadata(); | |
if (!$connection->tableExists('robots')) { | |
$connection->execute('CREATE TABLE robots (id integer primary key, name varchar(120) not null, type varchar(32) default "mechanical", year int default 2000)'); | |
} | |
class Robots extends Phalcon\Mvc\Model | |
{ | |
public function initialize() | |
{ | |
$this->skipAttributes(array('type', 'year')); | |
} | |
} | |
//Insert a record | |
$robot = new Robots(); | |
$robot->name = 'Hello ' . mt_rand(0, 100000); | |
$robot->save(); | |
//List records | |
echo '<table border="1">'; | |
foreach (Robots::find() as $robot) { | |
echo '<tr> | |
<td>', $robot->id, '</td> | |
<td>', $robot->name, '</td> | |
<td>', $robot->type, '</td> | |
<td>', $robot->year, '</td> | |
</tr>'; | |
} | |
echo '</table>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment