zend2数据层代码示例

zend2数据层配置和代码示范(带一对多关系)
表 admin, log,数据模型代码和工厂类,配置,控制器在模块Admin的调用示范

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
CREATE TABLE `admin` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自动编号',
  `username` varchar(64) NOT NULL COMMENT '用户名',
  `password` varchar(40) NOT NULL COMMENT '登陆密码',
  `disabled` tinyint(1) NOT NULL COMMENT '禁用状态',
  `name` varchar(64) NOT NULL COMMENT '姓名',
  `title` varchar(64) NOT NULL COMMENT '职位',
  `phone` varchar(64) NOT NULL COMMENT '联系电话',
  `email` varchar(320) NOT NULL COMMENT '邮件地址',
  `address` varchar(255) NOT NULL COMMENT '联系地址',
  `description` text NOT NULL COMMENT '其他记录',
  `addtime` bigint(20) NOT NULL COMMENT '增加时间',
  `uptime` bigint(20) NOT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `username_UNIQUE` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COMMENT='管理员数据表';
 
CREATE TABLE `adminlog` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自动编号',
  `admin` int(11) NOT NULL COMMENT '管理员',
  `content` text NOT NULL COMMENT '内容',
  `time` bigint(20) NOT NULL COMMENT '时间',
  PRIMARY KEY (`id`),
  KEY `fk_adminlog_admin_idx` (`admin`),
  CONSTRAINT `fk_adminlog_admin` FOREIGN KEY (`admin`) REFERENCES `admin` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=220 DEFAULT CHARSET=utf8 COMMENT='管理日志';
1
2
3
4
5
6
7
return array(
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
);

编辑 “/config/application.config.php” 注册数据库工厂服务

1
2
3
4
5
6
7
8
return array(
    'db' => array(
        'driver' => 'pdo',
        'platform' => 'Mysql',
        'pdodriver' => 'mysql',
        'options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8\'')
    ),
);

编辑 “/config/autoload/global.php” 配置数据库参数

1
2
3
4
5
6
7
8
return array(
    'db' => array(
        'hostname' => '服务器',
        'dbname' => '数据库名',
        'username' => '账号',
        'password' => '密码',
    ),
);

编辑 “/config/autoload/local.php” 配置数据库连接信息

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
namespace Admin;
 
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ServiceProviderInterface;
 
class Module implements ServiceProviderInterface {
 
    public function getServiceConfig() {
        return array("factories" => array(
                'Admin\Model\AdminTable' => function($serviceManager) {
            $resultset = new Model\Admin();
            $resultset->setServiceLocator($serviceManager)
            $resultSetPrototype = new ResultSet();
            $resultSetPrototype->setArrayObjectPrototype($resultset);
            return new Model\AdminTable(new TableGateway('admin', $serviceManager->get('Zend\Db\Adapter\Adapter'), null, $resultSetPrototype));
        },
                'Admin\Model\LogTable' => function($serviceManager) {
            $resultset = new Model\Log();
            $resultset->setServiceLocator($serviceManager)
            $resultSetPrototype = new ResultSet();
            $resultSetPrototype->setArrayObjectPrototype($resultset);
            return new Model\LogTable(new TableGateway('adminlog', $serviceManager->get('Zend\Db\Adapter\Adapter'), null, $resultSetPrototype));
        },
        ));
    }
}

编辑 “/module/Admin/Module.php” 将XXXTable作为服务注册. 并且注册时帮定数据实例(Model\XXX)类,$resultset->setServiceLocator($serviceManager) 是可选调用,用于给实例中的服务设置调用,如果数据行中用不上则可以不调用此行

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
 
namespace Admin\Model;
 
use Zend\ServiceManager\ServiceLocatorAwareInterface;
 
/**
 * entity of Admin
 *
 * @author ShiZhuolin
 */
class Admin implements ServiceLocatorAwareInterface {
 
    /**
     * 服务
     * @var \Zend\ServiceManager\ServiceLocatorInterface 
     */
    protected $servicelocator;
 
    /**
     * 自动编号
     * @var int 
     */
    public $id;
 
    /**
     * 用户名
     * @var string 
     */
    public $username;
 
    /**
     * 登陆密码
     * @var string
     */
    public $password;
 
    /**
     * 禁用状态
     * @var bool 
     */
    public $disabled;
 
    /**
     * 姓名
     * @var string 
     */
    public $name;
 
    /**
     * 职位
     * @var string 
     */
    public $title;
 
    /**
     * 联系电话
     * @var string 
     */
    public $phone;
 
    /**
     * 邮件
     * @var string 
     */
    public $email;
 
    /**
     * 联系地址
     * @var string 
     */
    public $address;
 
    /**
     * 描述
     * @var string 
     */
    public $description;
 
    /**
     * 增加时间
     * @var int 
     */
    public $addtime;
 
    /**
     * 更新时间
     * @var int 
     */
    public $uptime;
 
    /**
     * 交换数组数据
     * @param array $data
     * @return void
     */
    public function exchangeArray($data) {
        $this->id = isset($data['id']) ? $data['id'] : null;
        $this->username = isset($data['username']) ? $data['username'] : null;
        $this->password = isset($data['password']) ? $data['password'] : null;
        $this->disabled = isset($data['disabled']) ? $data['disabled'] : null;
        $this->name = isset($data['name']) ? $data['name'] : null;
        $this->title = isset($data['title']) ? $data['title'] : null;
        $this->phone = isset($data['phone']) ? $data['phone'] : null;
        $this->email = isset($data['email']) ? $data['email'] : null;
        $this->address = isset($data['address']) ? $data['address'] : null;
        $this->description = isset($data['description']) ? $data['description'] : null;
        $this->addtime = isset($data['addtime']) ? $data['addtime'] : null;
        $this->uptime = isset($data['uptime']) ? $data['uptime'] : null;
    }
 
    /**
     * 输出数组
     * @return array
     */
    public function getArrayCopy() {
        return array(
            'id' => $this->id,
            'username' => $this->username,
            'password' => $this->password,
            'disabled' => $this->disabled,
            'name' => $this->name,
            'title' => $this->title,
            'phone' => $this->phone,
            'email' => $this->email,
            'address' => $this->address,
            'description' => $this->description,
            'addtime' => $this->addtime,
            'uptime' => $this->uptime,
        );
    }
 
    /**
     * 记录日志
     * @param string $message
     */
    public function log($message) {
        $log = new \Admin\Model\Log;
        $log->admin = $this->id;
        $log->content = $message;
        $log->time = time();
        $this->getServiceLocator()->get('Admin\Model\LogTable')->insert($log);
    }
 
    public function getServiceLocator() {
        return $this->servicelocator;
    }
 
    public function setServiceLocator(\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator) {
        $this->servicelocator = $serviceLocator;
    }
 
}

编辑 “\module\Admin\src\Admin\Model\Admin.php” 数据实体

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
 
namespace Admin\Model;
 
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
 
/**
 * table of AdminTable
 * 
 * @author ShiZhuolin
 */
class AdminTable {
 
    /**
     * TableGateway
     * @var TableGateway
     */
    protected $tableGateway;
 
    /**
     * construct
     * @param TableGateway $tableGateway
     */
    public function __construct(TableGateway $tableGateway) {
        $this->tableGateway = $tableGateway;
    }
 
    /**
     * TableGateway
     * @return TableGateway
     */
    public function getTableGateway() {
        return $this->tableGateway;
    }
 
    /**
     * fetch data
     * @param Where|\Closure|string|array $where
     * @return ResultSet|Admin
     */
    public function fetch($where = null) {
        return $this->tableGateway->select($where);
    }
 
    /**
     * find by id
     * @param int $id
     * @return Admin
     */
    public function find($id) {
        return $this->tableGateway->select(array('id' => (int) $id))->current();
    }
 
    /**
     * insert data
     * @param Admin $admin
     * @return int
     */
    public function insert(Admin $admin) {
        $data = $admin->getArrayCopy();
        unset($data['id']);
        return $this->tableGateway->insert($data);
    }
 
    /**
     * update data
     * @param Admin $admin
     * @return int
     */
    public function update(Admin $admin) {
        $data = $admin->getArrayCopy();
        unset($data['id']);
        return $this->tableGateway->update($data, array('id' => (int) $admin->id));
    }
 
    /**
     * delete by id
     * @param int $id
     * @return int
     */
    public function delete($id) {
        return $this->tableGateway->delete(array('id' => (int) $id));
    }
 
}

编辑 “module\Admin\src\Admin\Model\AdminTable.php” 数据操作工厂.

1
$this->getServiceLocator()->get('Admin\Model\AdminTable')

在控制器中调用 AdminTable实例. 通过Tables实例操作数据库表

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.

Proudly powered by WordPress   Premium Style Theme by www.gopiplus.com