morphable / simple-database
A simple database component, easy to implement into any system
v1.0.4-stable
2019-03-15 10:30 UTC
Requires
- php: ~7.0
Requires (Dev)
- phpunit/phpunit: >=5.4.3
- squizlabs/php_codesniffer: ^2.3
This package is auto-updated.
Last update: 2024-12-16 04:17:03 UTC
README
A simple database component, easy to implement into any system
Usage
Before using the library, I will tell you this is a very limited ORM, I do this intentially because in my opinion it's better to write raw sql when you have complicated queries.
<?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();
Contributing
- Follow PSR-2 and the .editorconfig
- Start namespaces with \Morphable\SimpleDatabase
- Make tests