可变形/简单数据库

一个简单的数据库组件,易于集成到任何系统中

v1.0.4-stable 2019-03-15 10:30 UTC

This package is auto-updated.

Last update: 2024-09-16 03:36:30 UTC


README

一个简单的数据库组件,易于集成到任何系统中

用法

在使用库之前,我想告诉你这是一个非常有限的ORM,我故意这么做,因为在我看来,当你有复杂的查询时,直接写原始SQL会更好。

<?php

use \Morphable\SimpleDatabase;

$db = new SimpleDatabase($dsn, $user, $pass, $pdoOptions, function ($e) {
    // custom callback on query error, for instance to log failures
    die("error has occured " . $e->getMessage());
});

// execute raw query, returns PDOStatement
$stmt = $db->query("select ?", "1");
$stmt->fetch(\PDO::FETCH_COLUMN) // 1

// select
$select = $db->builder($table)
    ->select('`col1`, `col2`, `col3`')
    ->join('table on table.col1 = tale.col2')
    ->where('`col1` = ? and `col2` = ?', [$param1, $param2])
    ->orderBy('`col1` DESC')
    ->limit(5)
    ->offset(5)
    ->execute(); // returns QueryExecuter

$select->fetch(); // all rows
$select->fetchOne(); // first row
$select->fetchColumn(); // only possible when you only select 1 column
$select->getStatement(); // returns \PDOStatement

// insert
$insertId = $db->builder($table)
    ->insert([
        'col1' => $col1,
        'col2' => $col2,
        'col3' => $col3,
    ])
    ->execute()
    ->getLastInsertId();

// update
$db->builder($table)
    ->update([
        'col1' => $col1,
    ])
    // throws exception when you don't have a where clause
    ->where('`id` = ?', $id)
    ->execute();

// delete
$db->builder($table)
    ->delete()
    // throws exception when you don't have a where clause
    ->where('`id` = ?', $id)
    ->execute();

贡献

  • 遵循PSR-2和.editorconfig
  • 以\Morphable\SimpleDatabase开始命名空间
  • 编写测试