phpset/pdomodel

此包最新版本(v1.5)没有提供许可信息。

简单的基于PDO的模型

v1.5 2023-11-08 18:05 UTC

README

使用示例

class YoutubeVideosModel extends \PdoModel\PdoModel
{
    const TABLE = 'youtube_videos';

    protected function create($title, $src) {
        $this->insert(['title' => $title, 'src' => $src]);
    }
}
$youtubeVideosModel = new YoutubeVideosModel(new \Pdo());

$result = $youtubeVideosModel->select(['id', 'likes', 'url'])
    ->whereEqual('published', 1)
    ->where('likes', '>', 100)
    ->orderBy('likes desc')
    ->limit(100)
    ->offset(2000)
    ->groupBy('author')
    ->getAllRows();
var_dump($result);

设置

composer require phpset/pdomodel

检查您是否安装了正确的PHP扩展

# Change 8.2 below to your current PHP version
sudo apt install php8.2-pdo php8.2-pdo-mysql php8.2-pdo-sqlite

您需要创建一个普通的PDO连接

$connection = new \PDO(
    "mysql:host=127.0.0.1;dbname=YOURDBNAME;charset=utf8mb4",
    "YOURUSER",
    "YOURPASSWORD",
    [
            \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
            \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
    ]
);

在Symfony服务配置中创建PDO的示例

PDO:
  class: \PDO
  arguments:
    - "mysql:host=127.0.0.1;dbname=YOURDBNAME;charset=utf8mb4"
    - "YOURUSER"
    - "YOURPASSWORD"
    -
      !php/const PDO::ATTR_DEFAULT_FETCH_MODE: !php/const PDO::FETCH_ASSOC
      !php/const PDO::ATTR_ERRMODE: !php/const PDO::ERRMODE_EXCEPTION

SQLite示例

$connection = new \PDO('sqlite::db.sqlite');
new PdoModel($connection)->setTable('test_table');

测试

# run tests
./vendor/bin/phpunit

# Check coverage
php -dxdebug.mode=coverage ./vendor/bin/phpunit --coverage-text

灵感来源于