本文整理汇总了PHP中Phalcon\Di::getDefault方法的典型用法代码示例。如果您正苦于以下问题:PHP Di::getDefault方法的具体用法?PHP Di::getDefault怎么用?PHP Di::getDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Phalcon\Di
的用法示例。
在下文中一共展示了Di::getDefault方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addComment
public function addComment($data)
{
/** @var myModel $this */
$comment = new Comments();
$comment->content = $data['content'];
$comment->commentable_id = $this->id;
$comment->commentable_type = get_class($this);
// $comment->user_id = $this->getDI()->getShared('session')->get('auth')['id'];//获得当前登录对象的id
$user = \Phalcon\Di::getDefault()->get('auth');
$comment->user_id = $user->id;
//获得当前登录对象的id
// dd($comment);
$comment->save();
/** @var myModel $this */
if (method_exists($this, 'increaseCount')) {
$this->increaseCount('commentCount');
} else {
$this->save();
//更新时间
}
if (is_a($this, 'Tags')) {
$meta = $this->getTagmetaOrNew();
$meta->save();
}
return $this;
}
示例2: run
public static function run()
{
$di = \Phalcon\Di::getDefault();
$em = $di->get('em');
// Clear out any non-daily statistics.
$em->createQuery('DELETE FROM Entity\\Analytics a WHERE a.type != :type')->setParameter('type', 'day')->execute();
// Pull statistics in from influx.
$influx = $di->get('influx');
$daily_stats = $influx->setDatabase('pvlive_stations')->query('SELECT * FROM /1d.*/ WHERE time > now() - 14d', 's');
$new_records = array();
$earliest_timestamp = time();
foreach ($daily_stats as $stat_series => $stat_rows) {
$series_split = explode('.', $stat_series);
$station_id = $series_split[1] == 'all' ? NULL : $series_split[2];
foreach ($stat_rows as $stat_row) {
if ($stat_row['time'] < $earliest_timestamp) {
$earliest_timestamp = $stat_row['time'];
}
$new_records[] = array('station_id' => $station_id, 'type' => 'day', 'timestamp' => $stat_row['time'], 'number_min' => (int) $stat_row['min'], 'number_max' => (int) $stat_row['max'], 'number_avg' => round($stat_row['value']));
}
}
$em->createQuery('DELETE FROM Entity\\Analytics a WHERE a.timestamp >= :earliest')->setParameter('earliest', $earliest_timestamp)->execute();
foreach ($new_records as $new_record) {
$row = new \Entity\Analytics();
$row->fromArray($new_record);
$em->persist($row);
}
$em->flush();
}
示例3: getConnection
/**
* @return Client
*/
public function getConnection()
{
if (static::$connection === null) {
static::$connection = Di::getDefault()->getShared('Neo4jClient');
}
return static::$connection;
}
示例4: getDbName
public static function getDbName()
{
if (!isset(self::$_db)) {
self::$_db = Di::getDefault()->get('config')->mongodb->database;
}
return self::$_db;
}
示例5: getStatisticsForSchoolAndGrade
/**
* 根据学校id归类获取信息
*
* @param int $school_id
*
* @return mixed
* @author Hunter.<990835774@qq.com>
*/
public function getStatisticsForSchoolAndGrade($school_id = 0)
{
$page = new Page();
$page_table = $page->getSource();
$classes = new Classes();
$classes_table = $classes->getSource();
$eva_task_log = new EvaTaskLog();
$eva_task_log_table = $eva_task_log->getSource();
$sql_query = "select c.grade_id,count(DISTINCT p.id) as countP, 'reading' as type from {$page_table} as p\nleft join (\nSELECT grade_id,GROUP_CONCAT(id) as id_concat FROM {$classes_table}\nwhere school_id=:school_id\ngroup by grade_id\n) c on FIND_IN_SET(p.classes_id,c.id_concat)\nwhere is_finished=0\ngroup by c.grade_id\n\nunion all\n\nselect c.grade_id,count(DISTINCT p.id) as countP, 'reading_finished' as type from {$page_table} as p\nleft join (\nSELECT grade_id,GROUP_CONCAT(id) as id_concat FROM {$classes_table}\nwhere school_id=:school_id\ngroup by grade_id\n) c on FIND_IN_SET(p.classes_id,c.id_concat)\nwhere is_finished=1\ngroup by c.grade_id\n\nunion all\n\nselect c.grade_id,count(DISTINCT p.id) as countP, 'eva_task' as type from {$eva_task_log_table} as p\nleft join (\nSELECT grade_id,GROUP_CONCAT(id) as id_concat FROM {$classes_table}\nwhere school_id=:school_id\ngroup by grade_id\n) c on FIND_IN_SET(p.classes_id,c.id_concat)\ngroup by c.grade_id";
try {
if (DEBUG) {
$logger = Di::getDefault()->getLogger();
$logger->log($sql_query, \Phalcon\Logger::DEBUG);
}
$stmt = $this->di->getDefault()->get('db')->prepare($sql_query);
$stmt->bindParam(':school_id', $school_id, \PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
return $result;
} catch (\PDOException $e) {
if (DEBUG) {
die($e->getMessage());
} else {
$this->di->get('logger_error')->log('PDOException: ' . $e->getMessage(), \Phalcon\Logger::ERROR);
$this->di->get('dispatcher')->forward(['module' => 'home', 'controller' => 'error', 'action' => 'show_sql_error']);
}
}
}
示例6: isVotedBy
public function isVotedBy(Users $user = null)
{
if (null == $user) {
$user = \Phalcon\Di::getDefault()->get('auth');
}
return Vote::query()->where('voteable_type = :type:', ['type' => get_class($this)])->andWhere('voteable_id = :id:', ['id' => $this->id])->andWhere('user_id = :user:', ['user' => $user->id])->execute()->count() > 0;
}
示例7: run
public function run()
{
$log = new Stream('php://stdout');
/** @var Config $config */
$config = Di::getDefault()->getShared('config');
$expireDate = new DateTime('now', new DateTimeZone('UTC'));
$expireDate->modify('+1 month');
$baseUrl = rtrim($config->get('site')->url, '/');
$content = <<<EOL
User-agent: *
Allow: /
Sitemap: {$baseUrl}/sitemap.xml
EOL;
$adapter = new Local(dirname(dirname(__FILE__)) . '/public');
$filesystem = new Filesystem($adapter);
if ($filesystem->has('robots.txt')) {
$result = $filesystem->update('robots.txt', $content);
} else {
$result = $filesystem->write('robots.txt', $content);
}
if ($result) {
$log->info('The robots.txt was successfully updated');
} else {
$log->error('Failed to update the robots.txt file');
}
}
示例8: log
public static function log($type, $message = null, array $context = [])
{
static::$logger or static::$logger = Di::getDefault()->getShared('log');
$context['host'] = static::$hostname;
$context['request'] = fnGet($_SERVER, 'REQUEST_METHOD') . ' ' . fnGet($_SERVER, 'REQUEST_URI');
static::$logger->log($type, $message, $context);
}
示例9: sqlite
public function sqlite(UnitTester $I)
{
$I->wantToTest("Model validation by using SQLite as RDBMS");
/** @var \Phalcon\Di\FactoryDefault $di */
$di = Di::getDefault();
$connection = $di->getShared('db');
$di->remove('db');
$di->setShared('db', function () {
$connection = new Sqlite(['dbname' => TEST_DB_SQLITE_NAME]);
/** @var \PDO $pdo */
$pdo = $connection->getInternalHandler();
$pdo->sqliteCreateFunction('now', function () {
return date('Y-m-d H:i:s');
});
return $connection;
});
Di::setDefault($di);
$this->success($I);
$this->presenceOf($I);
$this->email($I);
$this->emailWithDot($I);
$this->exclusionIn($I);
$this->inclusionIn($I);
$this->uniqueness1($I);
$this->uniqueness2($I);
$this->regex($I);
$this->tooLong($I);
$this->tooShort($I);
$di->remove('db');
$di->setShared('db', $connection);
}
示例10: list
/**
* 跟find()类似,包含总数、页数、上一页、下一页等信息
*
* @param $parameters
* @param int $limit
* @param int $page
* @return mixed
*/
public static function list($parameters = null, $page = 1, $limit = 20)
{
// static function.
$di = Di::getDefault();
//
if (!is_array($parameters)) {
$params[] = $parameters;
} else {
$params = $parameters;
}
$manager = $di->getShared('modelsManager');
$builder = $manager->createBuilder($params);
$builder->from(get_called_class());
if (isset($params['limit'])) {
$limit = $params['limit'];
}
$params = ["builder" => $builder, "limit" => $limit, "page" => $page];
$paginator = $di->get("Phalcon\\Paginator\\Adapter\\QueryBuilder", [$params]);
$data = $paginator->getPaginate();
$items = [];
foreach ($data->items as $item) {
$items[] = $item->toArray();
}
$data = (array) $data;
$data['items'] = $items;
return $data;
}
示例11: connect
/**
* Opens a connection to the database, using dependency injection.
* @see \Phalcon\Queue\Db::diServiceKey
* @return bool
*/
public function connect()
{
if (!$this->connection) {
$this->connection = Di::getDefault()->get($this->diServiceKey);
}
return true;
}
示例12: __get
public function __get($propertyName)
{
$dependencyInjector = null;
$service = null;
$persistent = null;
$dependencyInjector = $this->_dependencyInjector;
if (!$dependencyInjector) {
$dependencyInjector = Di::getDefault();
}
if (!$dependencyInjector) {
throw new Exception("A dependency injection object is required to access the application services");
}
/**
* Fallback to the PHP userland if the cache is not available
*/
if ($dependencyInjector->has($propertyName)) {
$service = $dependencyInjector->getShared($propertyName);
$this->{$propertyName} = $service;
return $service;
}
if ($propertyName == "di") {
$this->{"di"} = $dependencyInjector;
return $dependencyInjector;
}
/**
* A notice is shown if the property is not defined and isn't a valid service
*/
trigger_error("Access to undefined property " . $propertyName);
return null;
}
示例13: __construct
/**
* Class constructor.
*
* @param array $options
* @param DbConnection $db
* @throws ValidationException
*/
public function __construct(array $options = [], DbConnection $db = null)
{
parent::__construct($options);
if (!$db) {
// try to get db instance from default Dependency Injection
$di = Di::getDefault();
if ($di instanceof DiInterface && $di->has('db')) {
$db = $di->get('db');
}
}
if (!$db instanceof DbConnection) {
throw new ValidationException('Validator Uniqueness require connection to database');
}
if (!$this->hasOption('table')) {
throw new ValidationException('Validator require table option to be set');
}
if (!$this->hasOption('column')) {
throw new ValidationException('Validator require column option to be set');
}
if ($this->hasOption('exclude')) {
$exclude = $this->getOption('exclude');
if (!isset($exclude['column']) || empty($exclude['column'])) {
throw new ValidationException('Validator with "exclude" option require column option to be set');
}
if (!isset($exclude['value']) || empty($exclude['value'])) {
throw new ValidationException('Validator with "exclude" option require value option to be set');
}
}
$this->db = $db;
}
示例14: getWatchList
public function getWatchList()
{
if (null == $this->watchList) {
$user = \Phalcon\Di::getDefault()->get('auth');
$this->watchList = Watchlist::findOrCreateNew($this, $user);
}
return $this->watchList;
}
示例15:
function __construct($id = null)
{
$this->redis = Di::getDefault()->getShared('redis');
//这里是否会出现一大堆connection呢?怎么能够查询出来呢?
if (method_exists($this, 'init') and null != $id) {
$this->init($id);
}
}