在ZendFramework中,可以將每張資料表的控制寫成Model,這樣的好處是可以讓Controller更為乾淨,也可以避免直接撰寫sql語法
如果透過zf tools建立整個流程會方便許多,以下範例皆是利用zf tools建立
0.資料庫schema及預設資料
1 | CREATE TABLE IF NOT EXISTS `admin` ( |
2 | ` id ` int(11) NOT NULL AUTO_INCREMENT, |
3 | `account` varchar(50) NOT NULL, |
4 | `password` varchar(50) NOT NULL, |
5 | `status` int(11) NOT NULL, |
7 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ; |
9 | INSERT INTO `admin` (` id `, `account`, `password`, `status`) VALUES |
10 | (1, 'johnson' , 'root123' , 1); |
1.資料庫設定
利用zf tools建立
2 | zf configure db-adapter 'adapter=PDO_MYSQL&dbname="test"&host="localhost"&username="root"&password="123"' production |
完成後應該可以看到application/configs/application.ini
1 | resources.db.adapter = "PDO_MYSQL" |
2 | resources.db.params.dbname = "test" |
3 | resources.db.params.host = "localhost" |
4 | resources.db.params.username = "root" |
5 | resources.db.params.password = "123" |
7 | resources.db.params.driver_options.1002 = "SET NAMES utf8" |
2.建立Table物件
利用zf tools建立
2 | zf create db-table Admin admin |
如果成功在application/models/DbTable會建立出一個Admin.php,並會有以下內容
1 | class Application_Model_DbTable_Admin extends Zend_Db_Table_Abstract |
3 | protected $_name = 'admin' ; |
3.建立Table Row Model
利用zf tools建立
1 | zf create model AdminRow |
成功的話路徑會是application/models/AdminRow.php,Row Model主要定義Row的資料和控制方法
1 | class Application_Model_AdminRow |
8 | public function __construct( $options = null) |
10 | if ( is_array ( $options )) { |
11 | $this ->setOptions( $options ); |
15 | public function __set( $name , $value ) |
17 | $method = 'set' . ucfirst( $name ); |
18 | if (!method_exists( $this , $method )) { |
19 | throw new Exception( 'Invalid property' ); |
21 | $this -> $method ( $value ); |
24 | public function __get( $name ) |
26 | $method = 'get' . ucfirst( $name ); |
27 | if (!method_exists( $this , $method )) { |
28 | throw new Exception( 'Invalid property' ); |
30 | return $this -> $method (); |
33 | public function setOptions( $options ) |
35 | $methods = get_class_methods( $this ); |
36 | foreach ( $options as $key => $value ) { |
37 | $method = 'set' . ucfirst( $key ); |
38 | if (in_array( $method , $methods )) { |
39 | $this -> $method ( $value ); |
45 | public function setId( $id ) |
51 | public function setAccount( $account ) |
53 | $this ->_account = $account ; |
57 | public function setPassword( $password ) |
59 | $this ->_password = $password ; |
63 | public function setStatus( $status ) |
65 | $this ->_status = $status ; |
69 | public function getId() |
74 | public function getAccount() |
76 | return $this ->_account; |
79 | public function getPassword() |
81 | return $this ->_password; |
84 | public function getStatus() |
86 | return $this ->_status; |
4.建立Table Mapper
利用zf tools建立
1 | zf create model AdminMapper |
成功的話路徑會是application/models/AdminMapper.php
在開發時主要使用此物件操作資料表,因此這個物件的方法就是隨著操作資料庫的需求自行撰寫
1 | class Application_Model_AdminMapper |
5 | public function setDbTable( $dbTable ) |
7 | if ( is_string ( $dbTable )) { |
8 | $dbTable = new $dbTable (); |
10 | if (! $dbTable instanceof Zend_Db_Table_Abstract) { |
11 | throw new Exception( 'Invalid table data gateway provided' ); |
13 | $this ->_dbTable = $dbTable ; |
18 | public function getDbTable() |
20 | if (null === $this ->_dbTable) { |
21 | $this ->setDbTable( 'Application_Model_DbTable_Admin' ); |
23 | return $this ->_dbTable; |
27 | public function save( $admin ) |
30 | 'id' => $admin ->getId(), |
31 | 'account' => $admin ->getAccount(), |
32 | 'password' => $admin ->getPassword(), |
33 | 'status' => $admin ->getStatus(), |
36 | foreach ( $data as $key => $val ) { |
42 | if (null === ( $id = $admin ->getId())) { |
43 | $this ->getDbTable()->insert( $data ); |
45 | $this ->getDbTable()->update( $data , array ( 'id = ?' => $id )); |
51 | public function find( $id ) |
53 | $result = $this ->getDbTable()->find( $id ); |
54 | if (0 == count ( $result )) { |
57 | $row = $result ->current(); |
58 | $admin = new Application_Model_AdminRow(); |
59 | $admin ->setId( $row ->id) |
60 | ->setAccount( $row ->account) |
61 | ->setPassword( $row ->password) |
62 | ->setStatus( $row ->status); |
67 | public function searchAccount( $account ) |
69 | if (null === $account ) { |
72 | $select = $this ->getDbTable()->select()->where( "account = ?" , $account ); |
73 | return $this ->fetchAll( $select ); |
76 | public function fetchAll( $select = null) |
79 | $resultSet = $this ->getDbTable()->fetchAll( $select ); |
81 | foreach ( $resultSet as $row ) { |
82 | $entry = new Application_Model_AdminRow(); |
83 | $entry ->setId( $row ->id) |
84 | ->setAccount( $row ->account) |
85 | ->setPassword( $row ->password) |
86 | ->setStatus( $row ->status); |
5.測試
在Controller中就可以測試是否成功連結
1 | $table_admin = new Application_Model_AdminMapper(); |
9 | $table_admin ->save( new Application_Model_AdminRow( $options )); |
16 | $table_admin ->save( new Application_Model_AdminRow( $options )); |
21 | print_r( $table_admin ->searchAccount( 'Hello' )); |
24 | print_r( $table_admin ->find(1)); |
27 | print_r( $result = $table_admin ->fetchAll()); |