zend2数据层配置和代码示范(带一对多关系)
表 admin, log,数据模型代码和工厂类,配置,控制器在模块Admin的调用示范
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='管理日志';
return array(
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
编辑 “/config/application.config.php” 注册数据库工厂服务
return array(
'db' => array(
'driver' => 'pdo',
'platform' => 'Mysql',
'pdodriver' => 'mysql',
'options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8\'')
),
);
编辑 “/config/autoload/global.php” 配置数据库参数
return array(
'db' => array(
'hostname' => '服务器',
'dbname' => '数据库名',
'username' => '账号',
'password' => '密码',
),
);
编辑 “/config/autoload/local.php” 配置数据库连接信息
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) 是可选调用,用于给实例中的服务设置调用,如果数据行中用不上则可以不调用此行
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” 数据实体
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” 数据操作工厂.
$this->getServiceLocator()->get('Admin\Model\AdminTable')
在控制器中调用 AdminTable实例. 通过Tables实例操作数据库表
Leave a Reply