SDpower

Program

MongoDB 新手入門筆記~

by on 七月.28, 2011, under PHP & MYSQL

Linux 安裝:

1
2
$ apt-get install mongodb-server mongodb-dev
$ sudo pecl install mongo

編輯php設定檔 加入 extension=mongo.so
php -m 即可以看到安裝好的擴充套件.

FreeBSD 安裝:

1
2
$ cd /usr/ports/databases/mongodb ; make install clean ;
$ cd /usr/ports/databases/pecl-mongo ; make install clean ;

php -m 即可以看到安裝好的擴充套件.
編輯/etc/rc.conf:

1
2
#mongodb     
mongod_enable="YES"

管理工具:
可先參考 Admin + UIs
個人推薦:Rock Mongo
JMongoBrowser
介面漂亮又方便.

Fork me on GitHub
Leave a Comment more...

Lua for PHP 安裝

by on 七月.20, 2011, under FreeBSD, Lua, PHP & MYSQL

主要安裝擴充套件資訊 LUA for PHP
使用 Plua 為安裝源.

Linux 安裝方式:
先安裝好 lua:

1
$apt-get install lua5.1 liblua5.1-0-dev

先下載並解開:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ cd /tmp
$ wget http://plua.googlecode.com/files/plua-1.0.0.zip
$ tar zxvf plua-1.0.0.zip
$ cd plua
$ phpize
$ whereis php-config
php-config: /usr/bin/php-config /usr/share/man/man1/php-config.1.gz
$ rm -f include
$ ln -s /usr/include/lua5.1 include
$ ln -s /usr/lib lib
$ cd lib
$ ln -s /usr/lib/liblua5.1.a liblua.a
$ ln -s /usr/lib/liblua5.1.so liblua.so
$ cd ..
$ make
$ make install
Installing shared extensions:     /usr/lib/php5/20090626/
$

自行編輯php 設定 增加extension=plua.so

FreeBSD 安裝方式:
先安裝好 lua:

1
$cd /usr/port/lang/lua ; make install clean ;

先下載並解開:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ cd /tmp
$ wget http://plua.googlecode.com/files/plua-1.0.0.zip
$ tar zxvf plua-1.0.0.zip
$ cd plua
$ phpize
$ whereis php-config
php-config: /usr/bin/php-config /usr/share/man/man1/php-config.1.gz
$ rm -f include
$ ln -s /usr/local/include/lua51 include
$ ln -s /usr/local/lib/lua51 lib
$ cd ..
$ make
$ make install
Installing shared extensions:     /usr/lib/php5/20090626/
$

自行編輯/usr/local/etc/php/extension.ini 設定 增加extension=plua.so

1 Comment more...

Doctrine 2 Stable Release!!

by on 十二月.29, 2010, under Doctrine

2010-12-21 這天 Doctrin2 釋出正式穩定版.
Doctrine 2 First Stable Release
這個版本工程之浩大於2008就開始開發,歷經2年半的時間努力.核心開發團隊五人於最後一年最後整合應用保有 Doctrine 1功能並重構並且導入新的DataMapper.

What is new in Doctrine 2?

* DQL is now a real language inside Doctrine, based on an EBNF that is parsed and transformed to SQL. Benefits of this refactoring are readable error messages, the generation of an AST that allows us to support many different vendors and powerful hooks for developers to modify and extend the DQL language to their needs. DQL can either be written as a string or be generated using a powerful QueryBuilder object.
* Your persistent objects (called entities in Doctrine 2) are not required to extend an abstract base class anymore. Doctrine 2 allows you to use Plain old PHP Objects.
* The UnitOfWork is not an alibi-pattern as implemented in Doctrine 1. It is the most central pattern in Doctrine 2. Instead of calling save() or delete() methods on your Doctrine_Record instances you now pass objects to the data mapper object called EntityManager and it keeps track of all changes until you request a synchronisation between database and the current objects in memory. This process is very efficient and has consistent semantics. This is a significant improvement over Doctrine 1 in terms of performance and developer ease-of-use.
* There are no code-generation steps from YAML to PHP involved in the library anymore. YAML, XML, PHP and Doc-Block Annotations are four first-class citizens for defining the metadata mapping between objects and database. A powerful caching layer allows Doctrine 2 to use runtime metadata without relying on code-generation.
* A clean architecture and powerful algorithms make Doctrine 2 magnitudes faster than Doctrine 1.
* Doctrine 2 supports an API that allows you to transform an arbitrary SQL statements into an object-structure. This feature is used by the Doctrine Query Language itself and is a first-class citizen of the library. It essentially allows you to make use of powerful vendor-specific features and complex SQL statements without having to cirumvent the ORM completely.
* Inheritance is not akward anymore. There are now three different types of inheritance to choose from: Mapped Superclasses, Single-Table- and Joined-Table-Inheritance.
* Many more features, just see the reference guide on what is possible with Doctrine 2.
(continue reading…)

Leave a Comment more...

Doctrine 入門(一)

by on 十二月.02, 2010, under Doctrine, Zend Framework

準備

依照之前整合的 Zend Framework 1.11.0 and Doctrine 1.2.3 and Smarty 3.0.4 環境,來做學習與測試.
更改index.php

< ?php
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    realpath(APPLICATION_PATH . '/../models'),
    realpath(APPLICATION_PATH . '/../models/generated'),
    get_include_path(),
)));

application.ini 環境

;Doctrine
autoloaderNamespaces[] = "Doctrine_"
pluginPaths.ZendX_Doctrine_Application_Resource = "ZendX/Application/Resource"
resources.doctrine.debug = 1
resources.doctrine.paths.models_path = APPLICATION_PATH "/../models"
resources.doctrine.connections.orm1.dsn.adapter = mysql
resources.doctrine.connections.orm1.dsn.user = "user"
resources.doctrine.connections.orm1.dsn.pass = "password"
resources.doctrine.connections.orm1.dsn.hostspec = "localhost"
resources.doctrine.connections.orm1.dsn.database = "orm1_sd_idv_tw"

檔案架構

(continue reading...)

2 Comments : more...

Zend Framework 1.11.0 and Doctrine 1.2.3 and Smarty 3.0.4

by on 十一月.20, 2010, under Doctrine, PHP & MYSQL, Zend Framework

懶惰許久~~ 趕快來補一下功課.
這篇主要介紹 Zend Framework 1.11.0 and Doctrine 1.2.3 and Smarty 3.0.4 的結合方式.
Doctrine 1.2.3 跟 Zend Framework 1.11.0 結合主要參考 http://pastie.org/481633 http://pastie.org/481635 使用.

目錄結構:

(continue reading…)

2 Comments more...

PHP: Doctrine

by on 九月.10, 2010, under Doctrine

Doctrine project 始於 2006 年,2008 年開始受到矚目;網站訪客數從每日 300 人到上千人;亦可在網上搜尋到許多 Framework 整合教學文章,例如 Zend FrameworkSymfonyCodeIgniter

Doctrine 的主要開發者 Jonathan H Wage,今年 24歲,非常年輕。Wage 在 Symfony 創始者的資助下成為 Doctrine 成立公司後的第一位員工。
(continue reading…)

Leave a Comment more...

PHP 5.3.3 FreeBSD 安裝調整.

by on 八月.10, 2010, under FreeBSD, PHP & MYSQL

php 5.3.3之後整合了php-fpm進去對於使用輕量化web server的使用者不再麻煩了.
以下是安裝畫面,安裝選項.

# cd /usr/ports/lang/php5; make install clean;


(continue reading…)

Leave a Comment more...

FreeBSD 上使用 wkhtmltopdf 做網頁擷取

by on 八月.04, 2010, under FreeBSD, PHP & MYSQL

以往要用程式控制將網頁輸出pdf或擷取網頁,都是件浩大工程.
這次介紹如何在FreeBSD 命令列模式下即可輕易辦到,並且不需龐大的X Windows 圖形系統適合在server上跑.

以下是軟體原文介紹.

wkhtmltopdf

Convert html to pdf using webkit (qtwebkit)

Description

Simple shell utility to convert html to pdf using the webkit rendering engine, and qt.

Introduction

Searching the web, I have found several command line tools that allow you to convert a HTML-document to a PDF-document, however they all seem to use their own, and rather incomplete rendering engine, resulting in poor quality. Recently QT 4.4 was released with a WebKit widget (WebKit is the engine of Apples Safari, which is a fork of the KDE KHtml), and making a good tool became very easy.
此軟體使用WebKit開發完成,除了flash以外其他接可正常顯示!!(包含js)
在安裝前請先確定你的FreeBSD已安裝 linux-base 套件並正常使用,並且將port tree更新.
(continue reading…)

2 Comments more...

Zend Framework multidb 不好用我自己來 Mudb

by on 四月.27, 2010, under PHP & MYSQL, Zend Framework

源由

Zend Framework 的 multidb 有用過因該知道,只是可以把多跟資料庫加入連結 要用時候在拿出其中之一DB來用.
有做過大型連結資料庫的使用著因該深有同感. 你們常做的動作並不是只要拿其中之一的db來做動作.
這樣動作只有在多個不同資料庫才有用而已.(如同時存取mysql mssql ….)

改進功能

  1. 讀寫分離.
  2. 讀取輪詢,分散式負載.
  3. 多重寫入

看到這邊不知道是否有引起你很大興趣呢?

除了讀寫分離,為何還需要讀取輪詢??

一般常用的資料庫,負載平衡主要也是為了分散負載.
不過你是否有跟我一樣困擾:

  • 已經分散了db讀取,卻需要每台server去分配db作為讀取.
  • 每台server都要不同的設定檔或環境,增加mis維護成本時間(煩不煩阿).
  • 某台slave db掛點,mis還須翻找server對應的db 表作為故障排除.當下還的傷透腦筋不知派哪個db給server用

你的福音來了!!使用他每台db使用固定對應表.

此套件運作模式分為Mw & RW 兩種.

Rw模式

可以設定多個matser和多個slave.
在使用下列取的資料的db function時視為 讀取動作

public function fetchAll($sql, $bind = array(), $fetchMode = null){}
public function fetchRow($sql, $bind = array(), $fetchMode = null){}
public function fetchAssoc($sql, $bind = array()){}
public function fetchCol($sql, $bind = array()){}
public function fetchPairs($sql, $bind = array()){}
public function fetchOne($sql, $bind = array()){}

在使用下列取的資料的db function時視為 寫入動作

public function insert($table, array $bind)
public function update($table, array $bind, $where = '')
public function delete($table, $where = '')

看範例說明比較快:
檔案application.ini

; multirwdb Databse setting
autoloadernamespaces[] = "Mudb"
pluginPaths.Mudb_Application_Resource = "Mudb/Application/Resource"
resources.multirwdb.model = "Rw"
resources.multirwdb.master[] = "db1"
resources.multirwdb.master[] = "db5"
resources.multirwdb.slave[] = "db2"
resources.multirwdb.slave[] = "db3"
resources.multirwdb.slave[] = "db4"
resources.multirwdb.db.db1.adapter = "pdo_mysql"
resources.multirwdb.db.db1.host     = "localhost"
resources.multirwdb.db.db1.username = "wacow"
resources.multirwdb.db.db1.password = "123456"
resources.multirwdb.db.db1.dbname   = "wacow"
resources.multirwdb.db.db1.charset  = "UTF8"
resources.multirwdb.db.db1.profiler.enabled = true
resources.multirwdb.db.db1.profiler.class = "Zend_Db_Profiler_Firebug"
 
resources.multirwdb.db.db2.adapter = "pdo_mysql"
resources.multirwdb.db.db2.host     = "localhost"
resources.multirwdb.db.db2.username = "wacow"
resources.multirwdb.db.db2.password = "123456"
resources.multirwdb.db.db2.dbname   = "wacow"
resources.multirwdb.db.db2.charset  = "UTF8"
resources.multirwdb.db.db2.profiler.enabled = true
resources.multirwdb.db.db2.profiler.class = "Zend_Db_Profiler_Firebug"
resources.multirwdb.db.db2.default = true
 
resources.multirwdb.db.db3.adapter = "pdo_mysql"
resources.multirwdb.db.db3.host     = "localhost"
resources.multirwdb.db.db3.username = "wacow"
resources.multirwdb.db.db3.password = "123456"
resources.multirwdb.db.db3.dbname   = "wacow"
resources.multirwdb.db.db3.charset  = "UTF8"
resources.multirwdb.db.db3.profiler.enabled = true
resources.multirwdb.db.db3.profiler.class = "Zend_Db_Profiler_Firebug"
resources.multirwdb.db.db3.default = true
 
resources.multirwdb.db.db4.adapter = "pdo_mysql"
resources.multirwdb.db.db4.host     = "localhost"
resources.multirwdb.db.db4.username = "wacow"
resources.multirwdb.db.db4.password = "123456"
resources.multirwdb.db.db4.dbname   = "wacow"
resources.multirwdb.db.db4.charset  = "UTF8"
resources.multirwdb.db.db4.profiler.enabled = true
resources.multirwdb.db.db4.profiler.class = "Zend_Db_Profiler_Firebug"
resources.multirwdb.db.db4.default = true
 
resources.multirwdb.db.db5.adapter = "pdo_mysql"
resources.multirwdb.db.db5.host     = "localhost"
resources.multirwdb.db.db5.username = "wacow"
resources.multirwdb.db.db5.password = "123456"
resources.multirwdb.db.db5.dbname   = "wacow"
resources.multirwdb.db.db5.charset  = "UTF8"
resources.multirwdb.db.db5.profiler.enabled = true
resources.multirwdb.db.db5.profiler.class = "Zend_Db_Profiler_Firebug"
resources.multirwdb.db.db5.default = true

檔案Bootstrap.php

<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	protected $_defaultDb = null;
	protected $_CacheDB = null;
	protected $_View = null;
	protected $_Customer = null;
	protected function _initDatabase() {
		if ($this->_defaultDb === null) {
			if ($this->hasPluginResource('multirwdb')) {
				$this->bootstrap('multirwdb');
				$resource = $this->getPluginResource('multirwdb');
				$this->_defaultDb = $resource->getDefaultDb();
			}elseif($this->hasPluginResource('db')){
				$this->bootstrap('db');
				$this->_defaultDb = $this->getPluginResource('db')->getDbAdapter();
			}
		}
		Zend_Registry::set('db',$this->_defaultDb);
	}
     protected function _initZFDebug() {
    if ($this->_defaultDb === null) {
			$this->_initDatabase();
    }
		$options = array(
	    	'plugins' => array(
				'Html',
				'Variables',
				'Memory',
				'Time',
				'Database' => ($this->hasPluginResource('multirwdb')) ? array('adapter' => MuDB_Db::getAllDb()) : array('adapter' => array('default' => $this->_defaultDb)),
				'File' => array('base_path' => realpath(APPLICATION_PATH . '/../')),
				'Registry',
				'Exception',
	 	));
    $debug = new ZFDebug_Controller_Plugin_Debug($options);
		$this->bootstrap('frontController');
		$frontController = $this->getResource('frontController');
		$frontController->registerPlugin($debug);
 
	}

檔案IndexController.php

<?php
class IndexController extends Zend_Controller_Action
{
    public function init()
    {
        $this->db=Zend_Registry::get('db');
    }
 
    public function indexAction()
    {
		$select_test = $this->db->select()->from('system_permissions', '*')->limit(100);
		$result = $this->db->fetchAll($select_test);
		$result = $this->db->fetchAll($select_test);
		$result = $this->db->fetchAll($select_test);
		$result = $this->db->fetchAll($select_test);
		$data= array('test' => rand(5, 15));
		$this->db->insert('test', $data);
    }
}

執行畫面解說:
在IndexController.php indexAction()下我們只是如同往常對單一的db做讀取動做.
總共做了4次,卻在不同db下查詢完成.
卻可以看到執行畫面下,自動輪詢查詢取的資料.
你是否可以發現程式設計師不用傷透腦筋去更改程式碼即可使用此套件即可完成!!
而寫入動作 $this->db->insert(‘test’, $data);
卻是對全部的MASTER做一併寫入更改!!

Mw模式

Mw模式基本上是Rw延伸
設定檔只存取master部分設定!!!
如範例檔案application.ini
只有db1 db5被引用
並將model改為Mw resources.multirwdb.model = "Mw"
而Mw模式下對db做讀取動做會做輪詢.(每個master都會輪詢等同Rw的slave)
寫入動作則一樣,全部寫入.

注意事項

不再列表的圖取寫入動作則做為對db直接的使用.
Rw模式會限制每個Slave db必須為相同的adapter,此處設限為了避免不小心混用db而渾然不知.
因為很多程式設計師select用法會而不同的資料庫而使用特殊語法.

寫入的特殊應用

而不管為Rw 或MW模式 對於寫入動作則不設限.
此方式可以實現有趣的功能 同時對不同的db來源(如同時對mssql 與mysql做同步更新 不過前提是兩邊資料表必須詳同),資料同步寫入變更!!!

總結

此套件花了近一個月修改,其中的經驗與處理模式不知是否讓你產生感慨~~
這些經驗都是爆肝來的,如有取用或發佈請保留引用來源.(檔案內並未有版權註解請隨意使用)
到這邊你以為沒了嗎???
其實還有西可以實現,如本來要加入的讀取權重(但是我不能再爆肝了 交由你吧QQ)
為何需要讀取權重??
很多系統會有開發報表等等做大量的資料處理(例如報表)
如果此時有權重分配即可像此套件的使用方式一樣不用額外定義,只需對大量的讀取另做讀取做權重區分!!

感謝收看 再會byby~~

附件檔案:Mudb.tar

1 Comment more...

Zend Framework + Smarty3 整合應用

by on 四月.08, 2010, under PHP & MYSQL, Zend Framework

承上篇 Zend Framework + Smarty2 + Smarty3 整合應用
我曾說過:之前有使用過ZFDebug 感受到整合便利性
所以對之前整合不甚滿意.
來看看最新版本~
將附件內的Smarty3拷貝到你的library目錄內
application.ini

autoloadernamespaces[] = "Smarty3"
pluginPaths.Smarty3_Application_Resource = "Smarty3/Application/Resource"
resources.smarty.script_path = APPLICATION_PATH "/"
resources.smarty.view_suffix = "tpl.htm"
resources.smarty.params.left_delimiter = "<%"
resources.smarty.params.right_delimiter = "%>"
resources.smarty.params.plugins_dir = APPLICATION_PATH "/lib/My/Smarty/plugins"
resources.smarty.params.compile_dir = APPLICATION_PATH "/../tmp/compiled"
resources.smarty.helper_dirs.key[] = "Zend/View/Helper"
resources.smarty.helper_dirs.value[] = "Zend_View_Helper_"
;如果將來需要自己的 View Helper ,可以放在這裡
resources.smarty.helper_dirs.key[] = "My/View/Helper"
resources.smarty.helper_dirs.value[] = "My_View_Helper_"

確認好樣板檔案格式,與目錄屬性.
然後Bootstrap.php呢???

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

你已經完成了!

這就是我要的整合~~

附件檔案:ZF_Smarty3.tar

5 Comments more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!