/** * Register the services here to make them general or register in the ModuleDefinition to make them module-specific */ protected function _registerServices() { $config = (include __DIR__ . "/../apps/config/config.php"); $di = new \Phalcon\DI\FactoryDefault(); $loader = new \Phalcon\Loader(); /** * We're a registering a set of directories taken from the configuration file */ $loader->registerDirs(array($config->application->libraryDir, $config->application->pluginDir))->register(); //Registering a router $di->set('router', function () { $router = new \Phalcon\Mvc\Router(); $router->setDefaultModule("frontend"); $router->add('/:controller/:action', array('module' => 'frontend', 'controller' => 1, 'action' => 2)); $router->add("/cats/index", array('module' => 'frontend', 'controller' => 'categories', 'action' => 'index')); $router->add("/cat/:params", array('module' => 'frontend', 'controller' => 'categories', 'action' => 'cat', 'params' => 1)); $router->add("/ask/:params", array('module' => 'frontend', 'controller' => 'ask', 'action' => 'ask', 'params' => 1)); $router->add("/admin/:controller/:action/:params", array('module' => 'backend', 'controller' => 1, 'action' => 2, 'params' => 3)); return $router; }); $di->set('db', function () use($config) { $mysql = new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name)); $mysql->query("set names 'utf8'"); return $mysql; }); $di->set('volt', function ($view, $di) use($config) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array('compiledPath' => $config->volt->path, 'compiledExtension' => $config->volt->extension, 'compiledSeparator' => $config->volt->separator, 'stat' => (bool) $config->volt->stat)); return $volt; }); //Set the views cache service $di->set('viewCache', function () { //Cache data for one day by default $frontCache = new Phalcon\Cache\Frontend\Output(array("lifetime" => 86400)); //Memcached connection settings $cache = new Phalcon\Cache\Backend\File($frontCache, array("cacheDir" => "../apps/caches/")); return $cache; }); /** * If the configuration specify the use of metadata adapter use it or use memory otherwise */ $di->set('modelsMetadata', function () use($config) { if (isset($config->models->metadata)) { $metaDataConfig = $config->models->metadata; $metadataAdapter = 'Phalcon\\Mvc\\Model\\Metadata\\' . $metaDataConfig->adapter; return new $metadataAdapter(); } else { return new Phalcon\Mvc\Model\Metadata\Memory(); } }); //Start the session the first time some component request the session service $di->set('session', function () { $session = new Phalcon\Session\Adapter\Files(); $session->start(); return $session; }); $this->setDI($di); }
public function searchAction() { $connection = new Phalcon\Db\Adapter\Pdo\Mysql(array("host" => "localhost", "username" => "root", "password" => "", "dbname" => "test_phalcon")); $value = $this->request->getPost("value"); $resultset = $connection->query("SELECT * FROM country WHERE name LIKE '%{$value}%'"); $countries = $connection->fetchAll("SELECT * FROM country WHERE name LIKE '%{$value}%' LIMIT 5", Phalcon\Db::FETCH_ASSOC); foreach ($countries as $country) { echo $country['name'] . "!!!"; } }
public function writeMysql($data_connector_id, $data, $queryType = "override", $primary_key = "auto") { $data_connector = DataConnector::findFirstByid($data_connector_id); $database = OrgDatabase::findFirstByorganisation_id($data_connector->organisation_id); $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array('host' => $database->db_host, 'username' => $database->db_username, 'password' => $database->db_password, 'dbname' => $database->db_name)); if ($queryType == "override") { $sql = "DROP TABLE IF EXISTS " . preg_replace("/[^A-Za-z0-9 ]/", "_", $data_connector->type) . "_" . $data_connector_id; } $sql = "CREATE TABLE IF NOT EXISTS " . preg_replace("/[^A-Za-z0-9 ]/", "_", $data_connector->type) . "_" . $data_connector_id . "("; foreach ($data['headings'] as $key => $column_name) { $type = gettype($data['0'][$key]); if ('string' == gettype($data['0'][$key])) { if (is_numeric($data['0'][$key])) { if ((int) $data['0'][$key] == (double) $data['0'][$key]) { $type = "integer"; } else { $type = "double"; } } else { if (strtotime($data['0'][$key]) != false) { $type = "date"; } else { } } } if ($type == "integer") { $sql = $sql . "`" . $column_name . "` int DEFAULT NULL, "; } elseif ($type == "double") { $sql = $sql . "`" . $column_name . "` real DEFAULT NULL, "; } elseif ($type == "date") { $sql = $sql . "`" . $column_name . "` datetime DEFAULT NULL, "; } else { $sql = $sql . "`" . $column_name . "` varchar(255) DEFAULT NULL, "; } } if ($primary_key == "auto") { $sql = $sql . "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY"; } elseif ($primary_key == "first") { $sql = $sql . "PRIMARY KEY (`" . reset($data['headings']) . "`)"; } else { $sql = $sql . "PRIMARY KEY (" . $primary_key . ")"; } $sql = $sql . ");"; $connection->query($sql); $rows = array(); $sql = "INSERT INTO " . preg_replace("/[^A-Za-z0-9 ]/", "_", $data_connector->type) . "_" . $data_connector_id . " (`" . implode("`,`", $data['headings']) . "`) VALUES "; foreach ($data as $row) { if (count($data['headings']) == count($row)) { $rows[] = "('" . implode("','", $row) . "')"; } } array_shift($rows); $sql = $sql . implode(", ", $rows) . " ON DUPLICATE KEY UPDATE "; $duplicate_values = array(); foreach ($data['headings'] as $column_name) { $duplicate_values[] = "`" . $column_name . "` =VALUES(`" . $column_name . "`)"; } $sql = $sql . implode(" ,", $duplicate_values) . ";"; $connection->query($sql); }
<li> <a href="#">Classement</a> </li> <li> <a href="#">Joueurs</a> </li> </ul> <ul class="nav navbar-nav navbar-right"> <li> <a href="#">Admin</a> </li> </ul> </div> </nav> <div class="atp-title"><h1>ATP</h1><h5>Phalcon MVC Application</h5></div> <div> <?php $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array('host' => 'localhost', 'dbname' => 'atp', 'username' => 'root', 'password' => 'Flocon123', 'options' => [PDO::ATTR_CASE => PDO::CASE_LOWER, PDO::ATTR_PERSISTENT => TRUE])); $result = $connection->query("SELECT * FROM joueur"); $result->setFetchMode(Phalcon\Db::FETCH_NUM); echo "<table class='table'><th>Flag</th><th>Nom</th><th>Prénom</th><th>Code Pays</th>"; while ($joueur = $result->fetch()) { echo "<tr><td class='flag'></td><td>{$joueur['2']}</td>"; echo "<td>{$joueur['1']}</td>"; echo "<td>{$joueur['3']}</td></tr>"; } echo "</table>"; ?> </div>
<?php try { $connection = new Phalcon\Db\Adapter\Pdo\Mysql(array('host' => '192.168.0.11', 'username' => 'sigma', 'password' => 'secret', 'dbname' => 'blog', 'port' => '3306')); $result = $connection->query("SELECT * FROM robots LIMIT 5"); $result->setFetchMode(Phalcon\Db::FETCH_NUM); while ($robot = $result->fetch()) { print_r($robot); } } catch (Phalcon\Db\Exception $e) { echo $e->getMessage(), PHP_EOL; }