Skip to content

Instantly share code, notes, and snippets.

@phalcon phalcon/where-in.php
Last active Dec 17, 2015

Embed
What would you like to do?
<?php
/**
mysql> CREATE TABLE `modules` (
-> `moduleid` smallint(5) NOT NULL AUTO_INCREMENT,
-> `parentid` smallint(5) DEFAULT NULL,
-> `title` varchar(50) DEFAULT NULL,
-> PRIMARY KEY (`moduleid`)
-> ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.21 sec)
mysql> insert into modules values (null, 1, 'Title 1');
Query OK, 1 row affected (0.00 sec)
mysql> insert into modules values (null, 1, 'Title 2');
Query OK, 1 row affected (0.00 sec)
mysql> insert into modules values (null, 2, 'Title 3');
Query OK, 1 row affected (0.00 sec)
*/
$di = new Phalcon\DI();
$db = new Phalcon\Db\Adapter\Pdo\Mysql(array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'test'
));
$eventsManager = new Phalcon\Events\Manager();
//Listen all the database events
$eventsManager->attach('db', function($event, $connection){
if ($event->getType() == 'beforeQuery') {
$sqlVariables = $connection->getSqlVariables();
if (count($sqlVariables)) {
echo $connection->getSqlStatement(), ' [', join(', ', $sqlVariables), ']<br>';
} else {
echo $connection->getSqlStatement(), '<br>';
}
}
});
//Assign the eventsManager to the db adapter instance
$db->setEventsManager($eventsManager);
$di['db'] = $db;
$di['modelsManager'] = new Phalcon\Mvc\Model\Manager();
$di['modelsMetadata'] = new Phalcon\Mvc\Model\MetaData\Memory();
//Define the class
class Modules extends Phalcon\Mvc\Model
{
}
$modules = $di['modelsManager']
->createBuilder()
->from('Modules')
->inWhere('parentid', array(1, 2))
->getQuery()
->execute();
foreach ($modules as $module) {
echo $module->parentid, ' ', $module->title, '<br>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.