slim:项目应用(二)数据库

项目应用(二)数据库

数据库使用doctrine orm,doctrine的依赖包含了后面会用到的symfony/console组件

  • 安装doctrine orm
composer require doctrine/orm
  • 新建cli配置
# config/cli-config.php


use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Slim\Container;

/** @var Container $container */
$container = require_once APP_ROOT . '/src/bootstrap.php';

ConsoleRunner::run(
    ConsoleRunner::createHelperSet($container[EntityManager::class])
);

  • 新建bootstrap
# src/bootstrap.php


use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Cache\FilesystemCache;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Tools\Setup;
use Slim\Container;

require_once APP_ROOT . '/vendor/autoload.php';

(new \Symfony\Component\Dotenv\Dotenv(false))->loadEnv(APP_ROOT . "/.env.local");

$env = $_ENV['APP_ENV']??'local';

$container = new Container(require APP_ROOT . "/config/config.$env.php");

$container[EntityManager::class] = function (Container $container): EntityManager {
    $config = Setup::createAnnotationMetadataConfiguration(
        $container['settings']['doctrine']['metadata_dirs'],
        $container['settings']['doctrine']['dev_mode']
    );

    $config->setMetadataDriverImpl(
        new AnnotationDriver(
            new AnnotationReader,
            $container['settings']['doctrine']['metadata_dirs']
        )
    );

    $config->setMetadataCacheImpl(
        new FilesystemCache(
            $container['settings']['doctrine']['cache_dir']
        )
    );

    return EntityManager::create(
        $container['settings']['doctrine']['connection'],
        $config
    );
};

return $container;
  • 新建可执行文件
# bin/doctrine


define('APP_ROOT', dirname(__DIR__));

@include APP_ROOT . "/vendor/bin/doctrine";
  • 新建entity
# 这是我的一个例子
# src/Entity/Data.php
# 里面的注释不能少,参考vendor/doctrine/annotations,文档:https://github.com/doctrine/annotations

/**
 * Created by PhpStorm.
 * Authon: akio 
 * Date: 7/31/19
 * Time: 11:26
 */

namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Annotations\Annotation;

/**
 * @author akio 
 * Class Data
 * @package App\Entity
 * CREATE TABLE `data` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `data` varchar(255) DEFAULT '',
    `age` tinyint(3) unsigned DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8mb4;
 * @ORM\Entity()
 * @ORM\Table(name="data")
 */

class Data
{

    /**
     * @author akio 
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @var int
     */
    protected $id;

    /**
     * @author akio 
     * @ORM\Column(type="string")
     * @var string
     */
    protected $data;

    /**
     * @author akio 
     * @ORM\Column(type="integer")
     * @var int
     */
    protected $age;

    /**
     * @author akio 
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @author akio 
     * @return string
     */
    public function getData(): string
    {
        return $this->data;
    }

    /**
     * @author akio 
     * @param string $data
     */
    public function setData(string $data): void
    {
        $this->data = $data;
    }

    /**
     * @author akio 
     * @return int
     */
    public function getAge(): int
    {
        return $this->age;
    }

    /**
     * @author akio 
     * @param int $age
     */
    public function setAge(int $age): void
    {
        $this->age = $age;
    }

}
  • 使用

参考src/controller/TestDoctrine.php

建议先了解doctrine orm的使用教程

  • PS
如果出现 'SQLSTATE[HY000] [2002] No such file or directory' 错误,请指定 php.ini 的 'pdo_mysql.default_socket' 配置

code

你可能感兴趣的:(php)