public function selectAllBy($where) { $metaData = new \Phalcon\Mvc\Model\MetaData\Memory(); $clumnTable = $metaData->getAttributes(new $this()); $clumnTable[] = 'us_email'; $query = self::query()->join('HaiQuan\\Backend\\Models\\CtgUserAdmin', 'u.us_id = HaiQuan\\Backend\\Models\\CtgTuVan.ch_user_id', 'u')->where($where)->columns($clumnTable)->orderBy("ch_date_create desc")->execute(); return $query; }
/** * Responds with columns about models * * @method mapColumn * @param object $model * @param integer $id * @return array */ public function mapColumn($model, $id) { $results = array(); $metaData = new \Phalcon\Mvc\Model\MetaData\Memory(); $columnMap = $metaData->getColumnMap($model); $result = $model::findFirst($id); foreach ($columnMap as $column) { $results[$column] = $result->{$column}; } return $results; }
/** * Initializes the database and metadata adapter */ public function init() { $di = $this->getDi(); $eventsManager = $this->getEventsManager(); $config = $this->_config; $adapter = $this->_getDatabaseAdapter($config->database->adapter); if (!$adapter) { throw new \Engine\Exception("Database adapter '{$config->database->adapter}' not exists!"); } $connection = new $adapter(["host" => $this->_config->database->host, "username" => $this->_config->database->username, "password" => $this->_config->database->password, "dbname" => $this->_config->database->dbname, "options" => [\PDO::ATTR_EMULATE_PREPARES => false]]); if (!$config->application->debug && $config->database->useCache) { if ($di->offsetExists('modelsCache')) { //$connection->setCache($di->get('modelsCache')); } } if ($config->application->debug) { // Attach logger & profiler $logger = new \Phalcon\Logger\Adapter\File($config->application->logger->path . "db.log"); $profiler = new \Phalcon\Db\Profiler(); $eventsManager->attach('db', function ($event, $connection) use($logger, $profiler) { if ($event->getType() == 'beforeQuery') { $statement = $connection->getSQLStatement(); $logger->log($statement, \Phalcon\Logger::INFO); $profiler->startProfile($statement); } if ($event->getType() == 'afterQuery') { //Stop the active profile $profiler->stopProfile(); } }); if ($this->_config->application->profiler && $di->has('profiler')) { $di->get('profiler')->setDbProfiler($profiler); } $connection->setEventsManager($eventsManager); } $di->set('db', $connection); if (isset($config->database->useAnnotations) && $config->database->useAnnotations) { $di->set('modelsManager', function () use($config, $eventsManager) { $modelsManager = new \Phalcon\Mvc\Model\Manager(); $modelsManager->setEventsManager($eventsManager); //Attach a listener to models-manager $eventsManager->attach('modelsManager', new \Engine\Model\AnnotationsInitializer()); return $modelsManager; }, true); } /** * If the configuration specify the use of metadata adapter use it or use memory otherwise */ $service = $this; $di->set('modelsMetadata', function () use($config, $service) { if ((!$config->application->debug || $config->application->useCachingInDebugMode) && isset($config->metadata)) { $metaDataConfig = $config->metadata; $metadataAdapter = $service->_getMetaDataAdapter($metaDataConfig->adapter); if (!$metadataAdapter) { throw new \Engine\Exception("MetaData adapter '{$metaDataConfig->adapter}' not exists!"); } $metaData = new $metadataAdapter($config->metadata->toArray()); } else { $metaData = new \Phalcon\Mvc\Model\MetaData\Memory(); } if (isset($config->database->useAnnotations) && $config->database->useAnnotations) { $metaData->setStrategy(new \Engine\Model\AnnotationsMetaDataInitializer()); } return $metaData; }, true); }
/** * Responds with information about updated inserted option * * @method put * @return json/xml data */ public function put($id) { $results = array(); $metaData = new \Phalcon\Mvc\Model\MetaData\Memory(); $request = $this->di->get('requestBody'); $option = Options::findFirstById($id); if (!$option) { throw new HTTPException("Not found", 404, array('dev' => 'Option does not exist', 'internalCode' => 'P1000', 'more' => '')); } else { $params['type'] = isset($request['type']) ? $request['type'] : $option->type; $params['name'] = isset($request['name']) ? $request['name'] : $option->name; $params['value'] = isset($request['value']) ? $request['value'] : $option->value; $params['label'] = isset($request['label']) ? $request['label'] : $option->label; $params['status'] = isset($request['status']) ? $request['status'] : $option->status; if ($option->save($params, $metaData->getColumnMap(new Options()))) { $results['id'] = $option->id; } else { throw new HTTPException("Request unable to be followed due to semantic errors", 422, array('dev' => $option->getMessages(), 'internalCode' => 'P1000', 'more' => '')); } } return $results; }
<?php $metaData = new Phalcon\Mvc\Model\MetaData\Memory(); $attributes = $metaData->getAttributes(new Robots()); print_r($attributes);
}); //注入gccrm数据库 $di->setShared('gccrm', function () use($di) { $dbclass = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $di['config']->v3crmDB->driver; return new $dbclass(array('host' => $di['config']->v3crmDB->host, 'username' => $di['config']->v3crmDB->username, 'password' => $di['config']->v3crmDB->password, 'dbname' => $di['config']->v3crmDB->database, 'charset' => $di['config']->v3crmDB->charset)); }); //临时用 $di->setShared('gccatetmp', function () use($di) { $dbclass = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $di['config']->v3catetmpDB->driver; return new $dbclass(array('host' => $di['config']->v3catetmpDB->host, 'username' => $di['config']->v3catetmpDB->username, 'password' => $di['config']->v3catetmpDB->password, 'dbname' => $di['config']->v3catetmpDB->database, 'charset' => $di['config']->v3catetmpDB->charset)); }); $di->setShared('modelsManager', function () { return new \Phalcon\Mvc\Model\Manager(); }); $di->set('modelsMetadata', function () { $metaData = new \Phalcon\Mvc\Model\MetaData\Memory(['prefix' => 'general']); $metaData->setStrategy(new Phalcon\Mvc\Model\MetaData\Strategy\Annotations()); return $metaData; }); //Annotations $di->set('annotations', function () { return new \Phalcon\Annotations\Adapter\Memory(); }); $di->setShared('remote', function () { $remote = new \Xz\Lib\Core\Remote(); return $remote; }); $di->setShared('local', function () { $local = new \Xz\Lib\Core\Local(); return $local; });
/** * Init database. * * @param DI $di Dependency Injection. * @param Config $config Config object. * @param EventsManager $eventsManager Event manager. * * @return Pdo */ protected function _initDb($di, $config, $eventsManager) { $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $config->db->mysql->adapter; /** @var Pdo $connection */ $connection = new $adapter(['host' => $config->db->mysql->host, 'port' => $config->db->mysql->port, 'username' => $config->db->mysql->username, 'password' => $config->db->mysql->password, 'dbname' => $config->db->mysql->dbname]); $isProfiler = $config->global->profiler; if ($isProfiler) { // Attach logger & profiler. $profiler = null; if ($isProfiler) { $profiler = new PhDbProfiler(); } $eventsManager->attach('db', function ($event, $connection) use($profiler) { if ($event->getType() == 'beforeQuery') { $statement = $connection->getSQLStatement(); if ($profiler) { $profiler->startProfile($statement); } } if ($event->getType() == 'afterQuery') { // Stop the active profile. if ($profiler) { $profiler->stopProfile(); } } }); if ($profiler && $di->has('profiler')) { $di->get('profiler')->setDbProfiler($profiler); } $connection->setEventsManager($eventsManager); } $di->set('db', $connection); /** * Add db service connect to five.vn database */ $di->set('dbfive', function () use($config) { $fiveAdapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $config->db->dbfive->adapter; return new $fiveAdapter(['host' => $config->db->dbfive->host, 'port' => $config->db->dbfive->port, 'username' => $config->db->dbfive->username, 'password' => $config->db->dbfive->password, 'dbname' => $config->db->dbfive->dbname]); }); $di->set('modelsManager', function () use($config, $eventsManager) { $modelsManager = new PhModelsManager(); $modelsManager->setEventsManager($eventsManager); // Attach a listener to models-manager $eventsManager->attach('modelsManager', new ModelAnnotationsInitializer()); return $modelsManager; }, true); /** * If the configuration specify the use of metadata adapter use it or use memory otherwise. */ $di->set('modelsMetadata', function () use($config) { if (ENV == ENV_PRODUCTION && isset($config->global->metadata)) { $metaDataConfig = $config->global->metadata; $metadataAdapter = '\\Phalcon\\Mvc\\Model\\Metadata\\' . $metaDataConfig->adapter; $metaData = new $metadataAdapter($config->global->metadata->toArray()); } else { $metaData = new \Phalcon\Mvc\Model\MetaData\Memory(); } $metaData->setStrategy(new PhStrategyAnnotations()); return $metaData; }, true); return $connection; }
<?php use Phalcon\Mvc\Router; use Phalcon\Mvc\View; date_default_timezone_set('US/Eastern'); setlocale(LC_ALL, 'ru_RU.UTF-8'); if (PHP_VERSION_ID < 50600) { iconv_set_encoding('internal_encoding', 'UTF-8'); } $parameters = (include_once __DIR__ . '/parameters.php'); return array('parameters' => &$parameters, 'services' => array('db' => array('class' => '\\Phalcon\\Db\\Adapter\\Pdo\\Mysql', '__construct' => array($parameters['db'])), 'logger' => array('class' => '\\Phalcon\\Logger\\Adapter\\File', '__construct' => array(APPLICATION_PATH . '/logs/' . APPLICATION_ENV . '.log')), 'url' => array('class' => '\\Phalcon\\Mvc\\Url', 'shared' => true, 'parameters' => $parameters['url']), 'tag' => array('class' => '\\App\\Tag'), 'modelsMetadata' => array('class' => function () { $metaData = new \Phalcon\Mvc\Model\MetaData\Memory(); $metaData->setStrategy(new \Engine\Db\Model\Annotations\Metadata()); return $metaData; }), 'dispatcher' => array('class' => function ($application) { $evManager = $application->getDI()->getShared('eventsManager'); $evManager->attach('dispatch:beforeException', function ($event, $dispatcher, $exception) use(&$application) { if (!class_exists('Frontend\\Module')) { include_once APPLICATION_PATH . '/modules/frontend/Module.php'; $module = new Frontend\Module(); $module->registerServices($application->getDI()); $module->registerAutoloaders($application->getDI()); } /** * @var $dispatcher \Phalcon\Mvc\Dispatcher */ $dispatcher->setModuleName('frontend'); $dispatcher->setParam('error', $exception); $dispatcher->forward(array('namespace' => 'Frontend\\Controller', 'module' => 'frontend', 'controller' => 'error', 'action' => 'index')); return false; });
/** * Init database. * * @param DI $di Dependency Injection. * @param Config $config Config object. * @param EventsManager $eventsManager Event manager. * * @return Pdo */ protected function _initDatabase($di, $config, $eventsManager) { if (!$config->installed) { return; } $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $config->database->adapter; /** @var Pdo $connection */ $connection = new $adapter(["host" => $config->database->host, "port" => $config->database->port, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname]); $isDebug = $config->application->debug; $isProfiler = $config->application->profiler; if ($isDebug || $isProfiler) { // Attach logger & profiler. $logger = null; $profiler = null; if ($isDebug) { $logger = new File($config->application->logger->path . "db.log"); } if ($isProfiler) { $profiler = new DatabaseProfiler(); } $eventsManager->attach('db', function ($event, $connection) use($logger, $profiler) { if ($event->getType() == 'beforeQuery') { $statement = $connection->getSQLStatement(); if ($logger) { $logger->log($statement, Logger::INFO); } if ($profiler) { $profiler->startProfile($statement); } } if ($event->getType() == 'afterQuery') { // Stop the active profile. if ($profiler) { $profiler->stopProfile(); } } }); if ($profiler && $di->has('profiler')) { $di->get('profiler')->setDbProfiler($profiler); } $connection->setEventsManager($eventsManager); } $di->set('db', $connection); $di->set('modelsManager', function () use($config, $eventsManager) { $modelsManager = new ModelsManager(); $modelsManager->setEventsManager($eventsManager); // Attach a listener to models-manager $eventsManager->attach('modelsManager', new ModelAnnotationsInitializer()); return $modelsManager; }, true); /** * If the configuration specify the use of metadata adapter use it or use memory otherwise. */ $di->set('modelsMetadata', function () use($config) { if (!$config->application->debug && isset($config->application->metadata)) { $metaDataConfig = $config->application->metadata; $metadataAdapter = '\\Phalcon\\Mvc\\Model\\Metadata\\' . $metaDataConfig->adapter; $metaData = new $metadataAdapter($config->application->metadata->toArray()); } else { $metaData = new \Phalcon\Mvc\Model\MetaData\Memory(); } $metaData->setStrategy(new StrategyAnnotations()); return $metaData; }, true); return $connection; }