All Projects → davebarnwell → model-orm-php

davebarnwell / model-orm-php

Licence: MIT License
PHP ORM Model class which provides table column/property mapping, CRUD, and dynamic finders/counters on a database table using PDO

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to model-orm-php

Database
💾 A database layer with a familiar PDO-like API but much more powerful. Building queries, advanced joins, drivers for MySQL, PostgreSQL, SQLite, MS SQL Server and Oracle.
Stars: ✭ 251 (+1294.44%)
Mutual labels:  orm, pdo
Sqlite orm
❤️ SQLite ORM light header only library for modern C++
Stars: ✭ 1,121 (+6127.78%)
Mutual labels:  crud, orm
Redux Orm
A small, simple and immutable ORM to manage relational data in your Redux store.
Stars: ✭ 2,922 (+16133.33%)
Mutual labels:  crud, orm
Flexicms
Flexible site management system Flexi CMS
Stars: ✭ 61 (+238.89%)
Mutual labels:  orm, pdo
Simple Crud
PHP library to provide magic CRUD in MySQL/Sqlite databases with zero configuration
Stars: ✭ 190 (+955.56%)
Mutual labels:  crud, orm
Php Sql Query Builder
An elegant lightweight and efficient SQL Query Builder with fluid interface SQL syntax supporting bindings and complicated query generation.
Stars: ✭ 313 (+1638.89%)
Mutual labels:  crud, pdo
Ezsql
PHP class to make interacting with a database ridiculusly easy
Stars: ✭ 804 (+4366.67%)
Mutual labels:  crud, pdo
Evolutility Server Node
Model-driven REST or GraphQL backend for CRUD and more, written in Javascript, using Node.js, Express, and PostgreSQL.
Stars: ✭ 84 (+366.67%)
Mutual labels:  crud, orm
Data
Fast DB-independent DAL for .NET Core: abstract queries, SQL commands builder, schema-less data access, POCO mapping (micro-ORM).
Stars: ✭ 150 (+733.33%)
Mutual labels:  crud, orm
Borm
【🔥今日热门】🏎️ 更好的ORM库 (Better ORM library that is simple, fast and self-mockable for Go)
Stars: ✭ 102 (+466.67%)
Mutual labels:  crud, orm
pinipig
🚀 Performant webservice framework
Stars: ✭ 25 (+38.89%)
Mutual labels:  crud, orm
Gosql
🐥The very simple ORM library for Golang
Stars: ✭ 233 (+1194.44%)
Mutual labels:  crud, orm
eloquent-mongodb-repository
Eloquent MongoDB Repository Implementation
Stars: ✭ 18 (+0%)
Mutual labels:  crud, orm
corma
Convention-based Object-Relational Mapper
Stars: ✭ 28 (+55.56%)
Mutual labels:  orm
fireo-nodejs
Google Cloud Firestore modern and simplest convenient ORM package in NodeJs. FireO is specifically designed for the Google's Firestore
Stars: ✭ 22 (+22.22%)
Mutual labels:  orm
CRUDReactNativeSQLite
CRUD de app em React Native utilizando armazenamento local com SQLite
Stars: ✭ 16 (-11.11%)
Mutual labels:  crud
FISCO-BCOS
FISCO BCOS是由微众牵头的金链盟主导研发、对外开源、安全可控的企业级金融区块链底层技术平台。 单链配置下,性能TPS可达万级。提供群组架构、并行计算、分布式存储、可插拔的共识机制、隐私保护算法、支持全链路国密算法等诸多特性。 经过多个机构、多个应用,长时间在生产环境中的实践检验,具备金融级的高性能、高可用性及高安全性。FISCO BCOS is a secure and reliable financial-grade open-source blockchain platform. The platform provides rich features including group architecture, cross-chain communication protoc…
Stars: ✭ 1,603 (+8805.56%)
Mutual labels:  crud
AbacusUtil
Release the power in Java programming
Stars: ✭ 77 (+327.78%)
Mutual labels:  orm
prophet
用于构建 react 中后台前端应用框架
Stars: ✭ 12 (-33.33%)
Mutual labels:  crud
graphql-tutorial
Tutorial for GraphQL
Stars: ✭ 24 (+33.33%)
Mutual labels:  orm

Model

Build Status

PHP Model class which provides

  • table column/property mapping,
  • CRUD
  • dynamic finders on a database table
  • dynamic counters on a database table
  • raw database queries with escaped parameters
  • helpers for finding one or more records returning rows as instances of the Model class
  • throws exception on query error
  • Requires PHP >= 5.3
  • Currenlty only tested with MySQL through the PDO drivers

Usage

With a mysql database as such on your localhost

CREATE DATABASE categorytest;
CREATE TABLE `categories` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(120) DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `created_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

require_once('vendor/autoload.php');
Db\Model::connectDb('mysql:dbname=categorytest;host=127.0.0.1','root','');    // db connection for all sub-classes

// minimum model definition
class Category extends db\Model {
  static protected $_tableName = 'categories'; // database table name
}

Save & Update records

$newCategory = new Category(array(
  'name' => 'test'
));
$newCategory->save();

as Id is not set inserts the data as a new table row, $newCategory->id is set that of the row inserted post insert

$newCategory->name = 'changed name';
$newCategory->save();

now updates existing record

new Category(array(
  'name' => 'second test'
))->save();

explict ->insert() and ->update() methods are available, ->save() is a wrapper around these

All methods call ->validate() before carrying out their operation, to do your own validation overide this method in your class and throw an Exception on validation error.

Find a single record by primary key

returns an object for the matching row

$cat = Category::getById(1);

Delete record(s)

Via an instance method

$cat = Category::getById(1);
$cat->delete();

OR a Class method (primary key deletes only)

Category::deleteById(1);

OR all records matching a where clause

Category::deleteAllWhere('name = ?',array('changed name'));

OR all records matching a where clause, specifying order and limits with more regular SQL

Category::deleteAllWhere('name = ? ORDER BY name DESC LIMIT 2',array('changed name'));

Dynamic field name finders & counters

Return an object for the first matching the name

Category::findOne_by_name('changed name');

Return an object for the first match from the names

Category::findOne_by_name(array('changed name','second test'));

Return an array of objects that match the name

Category::find_by_name('changed name');

Return an array of objects that match the names

Category::find_by_name(array('changed name','second test'));

Return the first record by ascending field 'name' as a Catgory object

Category::first_by_name('john');  // can also pass an array of values to match

Return the last record in the table when sorted by ascending field 'name' as a Catgory object

Category::last_by_name('john');   // can also pass an array of values to match

Return a count of records that match the name

Category::count_by_name('changed name');

Return a count of records that match a set of values

Category::count_by_name(array('changed name','second test'));

First, last & Count

return the first record by ascending primary key as a Catgory object

Category::first();

return the last record in the table when sorted by ascending primary key as a Catgory object

Category::last();

return the number of rows in the table

Catgory::count();

Arbitary Statements

run an arbitary statement returning a PDO statement handle to issue fetch etc... on

$st = db\Model::execute('SELECT * FROM categoies WHERE id = ? AND id = ? AND id > ?', array(1,2,6));

Find One Or All

custom SQL after the WHERE keyword returning the first match or all matches as Model instances

fetch one Category object with a custom WHERE ... clause

$cat = Category::fetchOneWhere('id = ? OR name = ?',array(1,'test'));

fetch array of Category objects with a custom WHERE ... clause

$cat = Category::fetchAllWhere('id = ? OR name = ?',array(1,'second test'));

fetch array of Category objects, as above but this time getting the second one if it exists ordered by ascending name

$cat = Category::fetchAllWhere('id = ? OR name = ? ORDER BY name ASC LIMIT 1,1',array(1,'second test'));

General SQL Helpers

Generate placeholders for an IN clause

$params = array(1,2,3,4);
$placeholders = db\Model::createInClausePlaceholders($params);    // returns a string '?,?,?,?
Category::fetchAllWhere('id IN ('.$placeholders.')',$params);     // use use in a query

Take a date string or unix datetime number and return a string that can be assigned to a TIMESTAMP or DATETIME field date strings are parsed into a unix date via PHPs incredibly flexible strtotime()

db\Model::datetimeToMysqldatetime('2012 Sept 13th 12:00');  // returns '2012-09-13 12:00:00'
db\Model::datetimeToMysqldatetime('next Monday');  // returns next monday midnight in the format 'YYYY-MM-DD HH:MM:SS'
db\Model::datetimeToMysqldatetime(gmdate());  // returns the current date time in the format 'YYYY-MM-DD HH:MM:SS'
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].