1:下载好zf2 web 应用
https://github.com/zendframework/ZendSkeletonApplication
用composer安装好
{
"name": "zendframework/skeleton-application",
"description": "Skeleton Application for ZF2",
"license": "BSD-3-Clause",
"keywords": [
"framework",
"zf2"
],
"homepage": "http://framework.zend.com/",
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": "~2.4.11",
"doctrine/doctrine-module": "0.9.*",
"doctrine/doctrine-orm-module": "0.9.*"
}
}
项目结构
2:配置好doctrine参数
'modules' => array(
'DoctrineModule',
'DoctrineORMModule',
'Application',
),
配置数据库地址
return array(
'doctrine' => array(
'connection' => array(
// default connection name
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => '127.0.0.1',
'port' => '3306',
'user' => 'root',
'password' => '123456',
'dbname' => 'zhdc',
'charset' => 'utf8',
'driver_options' => array(
1002 => 'SET NAMES utf8',
),
),
)
)
),
);
配置实体类路径
'doctrine' => array(
'driver' => array(
// defines an annotation driver with two paths, and names it `my_annotation_driver`
'Application_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
__DIR__ . '/../src/Application/Entity',
),
),
// default metadata driver, aggregates all other drivers into a single one.
// Override `orm_default` only if you know what you're doing
'orm_default' => array(
'drivers' => array(
// register `my_annotation_driver` for any entity under namespace `My\Namespace`
'Application\Namespace' => 'Application_driver'
)
)
)
),
运行doctrine-model命令
./vendor/bin/doctrine-module
创建数据库表实体类
<?php
/**
* Created by PhpStorm.
* User: zhudechao
* Date: 2019/1/11
* Time: 下午12:28
*/
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
class Users
{
/**
* @ORM\Column(name="id", type="integer", options={"comment"=""})
*/
protected $id;
/**
* @ORM\Column(name="name", type="string", length=255, nullable=true, options={"comment"=""})
*/
protected $name;
/**
* @ORM\Column(name="num", type="integer", options={"comment"=""})
*/
protected $num;
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setNum($num)
{
$this->num = $num;
return $this;
}
public function getNum()
{
return $this->num;
}
}
准备完毕,开始查询数据库
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function indexAction()
{
return new ViewModel();
}
public function aAction()
{
$em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
//PDO
$conn = $em->getConnection();
$res = $conn->prepare('SELECT * FROM tb_books');
$res->execute();
$red = $res->fetchAll();
foreach($red as $data){
echo $data['name'].' - '.$data['author'];
echo '<br>';
}
exit;
}
}
数据库表结构
效果
链接:https://pan.baidu.com/s/1tNQoOUobTafuKuqlQ-TeKw 密码:i8n0