Zend Framework2 会员管理项目之一:初始化数据库与配置

2014年03月01日

本文假设Zend Framework 2 的运行环境已经搭建好。
本会员管理构架步骤如下:

一,创建数据库,初始化数据表

创建数据库:qiais,登录数据库的用户与密码:qiais:qiais312

# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3020
Server version: 5.1.69 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database qiais default character set utf8 collate utf8_gereral_ci;
mysql> grant all on qiais.* to qiais@localhost identified by 'qiais';
mysql> flush privileges;

创建数据库表,导入数据

DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `role` varchar(10) COLLATE utf8_unicode_ci DEFAULT 'member',
  `department_id` int(4) DEFAULT '0',
  `username` varchar(60) CHARACTER SET utf8 DEFAULT NULL,
  `password` varchar(60) CHARACTER SET utf8 DEFAULT NULL,
  `name` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `gender` tinyint(1) DEFAULT '0',
  `email` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
  `mobile` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
  `tel` varchar(20) CHARACTER SET utf8 DEFAULT NULL,
  `fax` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `address` varchar(250) CHARACTER SET utf8 DEFAULT NULL,
  `zipcode` varchar(20) CHARACTER SET utf8 DEFAULT NULL,
  `memo` varchar(300) CHARACTER SET utf8 DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  `deleted` tinyint(1) DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=49 ;

--
-- テーブルのデータのダンプ `users`
--

INSERT INTO `users` (`id`, `role`, `department_id`, `username`, `password`, `name`, `gender`, `email`, `mobile`, `tel`, `fax`, `address`, `zipcode`, `memo`, `created`, `modified`, `deleted`) VALUES
(1, 'member', 1, 'saegusa', 'd74ddf6a8e106c6c0d0c0514d12967bd4aafbdd5', '张天明', 0, 'zhangtianming@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(2, 'member', 1, 'sugahara', '00fc474e05d0ab84ce79edd19e417d540d0f190e', '王小二', 0, 'sugahara@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(3, 'member', 1, 'miyayama', '58404c6a7c6fab7cc53a92511ed699945bb0672f', '李国立', 0, 'miyayama@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(4, 'member', 1, 'ogawa', '77a1a6222d343d5fe5e351cde1798d7b83f9f23b', '陈龙', 0, 'ogawa@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(5, 'member', 1, 'sagawa', '44db127c92f133f5d36a40bd8c73b6847beaa380', '艾心', 0, 'sagawa@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(6, 'member', 1, 't.itoh', '966cb5334889eaa9a5a6c9e39264cd7182337757', '伊東', 0, 't.itoh@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(7, 'member', 1, 'h.takeda', '502119e44b50ff25775650843b7da0d052ba269d', '竹田', 0, 'h.takeda@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(8, 'member', 1, 'sai', '7def0fd47e50c92e3b5a965e569bab5ff4d28660', '斉 街栄', 0, 'sai@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 10:20:32', 0),
(9, 'member', 1, 'h.suzuki', 'df3576b7ef690403ad5e9dfb4a89d93e88d9dd31', '鈴木', 0, 'h.suzuki@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(10, 'member', 1, 'kawaguchi', '038f9390e511855db7cc034b7ffa6d14697f0565', '川口', 0, 'kawaguchi@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(11, 'member', 1, 'kashiwabara', '1edcc5b45f7838703ede53475c9ecf9f71460bdc', '柏原', 0, 'kashiwabara@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(12, 'member', 4, 'kosugi', 'c18869fb60dcba2971940b0f7b471862611d57c6', '小杉', 0, 'kosugi@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(13, 'member', 4, 'a.yamada', 'a18816edf1b56f74b7f696ba3146bf6d2fd50017', '山田', 0, 'a.yamada@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(14, 'member', 4, 'm.takahashi', 'd45638c044b11537c3f8d0feffc7e588029dc0cf', '高橋', 0, 'm.takahashi@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(15, 'member', 4, 'r.satoh', 'e44a930af3ddf679ff3fddbb38df30a24d8c20de', '武田', 0, 'r.satoh@qiais.com', '080-7022-8514', '', NULL, '東京都足立区千住5-1-3 ガクエンヒルズ北千住804', '120-0034', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(16, 'member', 4, 'nakayama', 'c11c9e6efb59f84b338136be6ea52f42f9c91d65', '中山', 0, 'nakayama@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(17, 'member', 4, 'teduka', '5ef572d0e845739cefaf8dcbe5a06ab41fe8bdea', '手塚', 0, 'teduka@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(18, 'member', 5, 'ishii', '0485a322b64d2b8b8dd60e1a7e728ad3230721c1', '石井', 0, 'ishii@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-16 02:17:59', 0),
(19, 'member', 5, 't.yamada', 'b7990b482eb77db423717875a9eb89bd1e282e14', '曹操', 0, 'caocao@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-16 02:18:23', 0),
(20, 'member', 4, 'k.suzuki', 'e89a8166c29d7d1f85d09654b3d4783f6522a00f', '薛仁贵', 0, 'xuerengui@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(21, 'member', 5, 'gunji', '36f5a9d6323b885aa6881fd9fbd6595887df937a', '程咬金', 0, 'chengyaojin@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-16 02:18:43', 0),
(22, 'member', 5, 'hori', '09a58fb3f45bb2d60af8fa16ecea73453e4058e3', '李世民', 0, 'lishimin@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-16 02:18:52', 0),
(23, 'member', 5, 'shimada', '11e721609d65f0d007f0636fbc84171bd522e79c', '島田', 0, 'shimada@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-16 02:19:01', 0),
(24, 'member', 5, 't.suzuki', '174bea76b6205f27d09c2a5563dbe9743af06c9b', '鈴木', 0, 't.suzuki@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-16 02:19:14', 0),
(25, 'member', 1, 'nakanishi', '885f1e728418ac205e3fc09142bdda164cc28cad', '中西', 0, 'nakanishi@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(26, 'member', 2, 'sakai', '629afd3de3eb222b88afad6f362ab318f44f2d1a', '酒井', 0, 'sakai@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(27, 'member', 5, 'umemoto', 'de57902f8f02182df45b5f6dadbde60900862963', '梅本', 0, 'umemoto@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-16 02:19:30', 0),
(28, 'member', 5, 'isobe', 'b9a782712864fd39abab7be5c48a27494afdbe43', '磯部', 0, 'isobe@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-16 02:19:44', 0),
(29, 'member', 5, 'awano', 'f3aaa9b9938d51419fba3202bf6d1c3dae6b3634', '粟野', 0, 'awano@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-16 02:20:03', 0),
(30, 'member', 3, 'matsuoka', '73bd07b6be35498df88eea21e1ffb30ccfddf064', '松岡', 0, 'matsuoka@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(31, 'member', 3, 'toda', 'd2749b23da3ae863db392ed0b59e46e17a684120', '戸田', 0, 'toda@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(32, 'member', 3, 'hamada', 'c8f4c129e5dd7292c8382b57561317048a10b706', '浜田', 0, 'hamada@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(33, 'member', 3, 'ohhira', 'f09b9618f82ca1643d91cdc1c12e47682a296a77', '大平', 0, 'ohhira@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(34, 'member', 3, 'h.katoh', '995d8a9417fd41146a3684e2ad6599162473fc41', '加藤', 0, 'h.katoh@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(35, 'member', 3, 'kishino', '38a26c4d27ea76f747e34b243f83005dbde38af1', '木下', 0, 'kinoshita@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(36, 'member', 3, 'nishiyama', 'cbf860f78c6f03ec02cc9cbf7675fc4f48e21df0', '西山', 0, 'nishiyama@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(37, 'member', 2, 'yamashita', '4f5a18a639a01126c1e11a71a2b969914fad50dd', '山下 功', 0, 'yamashita@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(38, 'member', 2, 'nakatani', '41895caaa4168cdc29a614793c93564833d51e4f', '中谷', 0, 'nakatani@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(39, 'member', 2, 'h.nakamura', 'f19ff6348cbcbd7c5d2712af233787549bd25893', '中村', 0, 'h.nakamura@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-17 06:59:10', 0),
(40, 'member', 2, 'y.satoh', 'fc367507fd8dfbd093073187e854440a287607b7', '佐藤', 0, 'y.satoh@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(41, 'member', 2, 'teshigawara', 'f1605a1f5875936fda6105b390adce1b95b61723', '昭和', 0, 'zhaohe@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(42, 'member', 2, 'e.fujita', '47f63dc3bc801a05486ca030b298873a1df537e4', '藤田', 0, 'e.fujita@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(43, 'member', 2, 'aritaki', '303a86d8f5898ed87be118c738fdce3db7fea1b3', '有滝', 0, 'aritaki@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-31 07:11:54', 0),
(44, 'member', 2, 'usuba', '010e3196e1f0cec1da35fe1d9a05a9f12d3d5130', '薄葉', 0, 'usuba@qiais.com', '', '', NULL, '', '', '', '2012-12-19 17:38:54', '2014-01-15 08:29:02', 0),
(45, 'member', 5, 't.kobayashi', '8f66ab1264cf52659e62fe8d4c48b0490c7c54cc', '小林', 0, 't.kobayashi@qiais.com', '', '', NULL, '', '', '', '2013-02-14 17:27:22', '2014-01-16 02:20:35', 0),
(46, 'member', 1, 'fukui', 'c16298e93b6d145adb670aa26005b3f9385b2f28', '福井', 0, 'fukui@qiais.com', '', '', NULL, '', '', '', '2013-04-24 15:19:10', '2014-01-15 08:29:02', 0),
(47, 'member', 5, '', '0d0cd58f1d09483cc5886e1f575e03a49f6c779a', '吴静安', 0, 'wujingan@qiais.com', '', '', NULL, '', '', '', '2014-01-16 02:01:08', '2014-01-16 02:20:49', 0),
(48, 'member', 5, '', 'dace7a7d4f8ba9545f37b99802fa567d41c7bef6', '山口洋介', 0, 'yamaguchi@qiais.com', '', '', NULL, '', '', '', '2014-01-16 02:04:08', '2014-01-16 02:17:25', 0);

二,配置zf2,数据库连接

config/autoload/global.php

return array(
    'db' => array(
        'driver' => 'Pdo',
        'dsn' => 'mysql:dbname=qiais_cn;host=localhost',
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
        'aliases' => array(
            'db' => 'Zend\Db\Adapter\Adapter'
        )
    ),
);

将用户名与密码配置在local.php
config/autoload/local.php

return array(
    'db' => array(
        'username' => 'qiais',
        'password' => 'qiais312',
    )
);