<?php
abstract class UMA_Plugin_Mongo
{
protected $name = null;
private $_db;
//单例模式
private static $_dbinstance;
public static function getDbInstance()
{
if(self::$_dbinstance == null)
{
$_dbinstance = new iDatabase(IDATABASE_PROJECT_ID, IDATABASE_PASSWORD);
}
return $_dbinstance;
}
public function __construct () {
$this->_db = $this->getDbInstance();//Zend_Registry::get('mongoDB');
}
public function getDB() {
return $this->_db;
}
public function find($query=array(),$sort = array("_id"=>1),$skip=0,$limit=10000) {
return $this->_db->find($this->name, $query, $sort, $skip, $limit);
}
public function findOne($query) {
return $this->_db->findOne($this->name, $query);
}
public function findAndModify($options) {
return $this->_db->findAndModify($this->name, $options);
}
public function count($query) {
return $this->_db->count($this->name, $query);
}
public function remove($query) {
return $this->_db->remove($this->name, $query);
}
public function insert ($object) {
return $this->_db->insert($this->name, $object);
}
public function update ($criteria, $object)
{
return $this->_db->update($this->name, $criteria, $object);
}
public function aggregate(array $ops) {
return $this->_db->aggregate($this->name, $ops);
}
public function getCondition($condition = null)
{
$query= array();
if(!empty($condition)){
foreach($condition as $k=>$v){
if(!empty($v)){
$query[$k] = trim($v);
}
}
}
return $query;
}
/**
* Load Smarty library
*/
public static function LoadSmarty()
{
require_once(SMARTY_DIR.'Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = BASE_DIR.'templates/';
$smarty->compile_dir = BASE_DIR.'templates_c/';
$smarty->left_delimiter = "{{";
$smarty->right_delimiter = "}}";
$smarty->allow_php_tag=true;
return $smarty;
}
public static function LoadMySmarty()
{
require_once(SMARTY_DIR.'MySmarty.php');
$mysmarty = new MySmarty();
$mysmarty->template_dir = BASE_DIR.'templates/';
$mysmarty->compile_dir = BASE_DIR.'templates_c/';
$mysmarty->left_delimiter = "{{";
$mysmarty->right_delimiter = "}}";
$mysmarty->allow_php_tag=true;
return $mysmarty;
}
public static function LoadFront1126Smarty()
{
require_once(SMARTY_DIR.'Front1126Smarty.php');
$mysmarty = new Front1126Smarty();
$mysmarty->template_dir = BASE_DIR.'templates/';
$mysmarty->compile_dir = BASE_DIR.'templates_c/';
$mysmarty->left_delimiter = "{{";
$mysmarty->right_delimiter = "}}";
$mysmarty->allow_php_tag=true;
return $mysmarty;
}
public static function LoadFrontSmarty()
{
require_once(SMARTY_DIR.'FrontSmarty.php');
$fsmarty = new FrontSmarty();
$fsmarty->template_dir = BASE_DIR.'templates/';
$fsmarty->compile_dir = BASE_DIR.'templates_c/';
$fsmarty->left_delimiter = "{{";
$fsmarty->right_delimiter = "}}";
$fsmarty->allow_php_tag=true;
return $fsmarty;
}
}
//--------------------------------------------------------
class ConnectionFactory
{
private static $factory;
public static function getFactory()
{
if(!self::$factory)
self::$factory =new ConnectionFactory(...);
return self::$factory;
}
private $db;
public function getConnection()
{
if(!$db)
$db =new PDO(...);
return $db;
}
}
//调用
function getSomething()
{
$conn =ConnectionFactory::getFactory()->getConnection();...
//-----------------------------------------实例二------------------------------------------
class Database Singleton
{
// [Singleton]
private static $instance =null;
public static function getInstance()
{
if(!self::$instance)
{
self::$instance =new self();
}
return self::$instance;
}
privatefunction __clone(){}
// [/Singleton]
private $connection =null;
private function __construct()
{
$this->connection = mysql_connect('localhost','root','admin');
if($this->connection)
{
mysql_select_db('my_database');
}
}
//
// crud operations go here.
//
}
$db =DatabaseSingleton::getInstance();
$db->SomeCRUDOperation();
//----------------------------------实例三-------------------------------------
class Database
{
private static $instance =null;
private static $conn;
private function __construct()
{
self::$conn =new mysqli('localhost','root','root','database','3307');
}
public static function getInstance()
{
if(self::$instance ==null){
self::$instance =new Database();
}
return self::$instance;
}
public function query($sql)
{
return self::$conn->query($sql);
}
}