onesimus-systems/seed-catalog

1.1.0 2015-10-11 01:19 UTC

This package is auto-updated.

Last update: 2024-09-09 04:37:18 UTC


README

Build Status

Seed Catalog是一个简单的PHP数据库接口。它的目标是使与数据库的最常见交互更加容易。我已经使用它有一段时间了。我觉得是时候分享它了。

Seed Catalog是erusev的Base项目的分支。

特性

  • 简单
  • 直观
  • 独立
  • 安全
  • 基于PDO
  • 在5.3、5.4、5.5、5.6和HHVM上测试过

安装

包含SC.phpCollection.php和'SCException.php',或者安装composer包

示例

连接到数据库

# initialize the connection
# connect() will return false if connecting to the database fails.
# connect($dbtype, $host, $database, $username, $password)

$SC = new SC\SC();
$SC->connect('mysql', 'localhost', 'example', 'username', 'password');

# you can also give it a PDO object to use directly

$SC = new SC\SC($pdo);

处理记录

# read user 1
$SC->readItem('user', 1);
# update the username of user 1
$SC->updateItem('user', 1, ['username' => 'john.doe']);
# create a user
$SC->createItem('user', ['username' => 'jane.doe', 'email' => 'jane@example.com']);
# delete user 1
$SC->deleteItem('user', 1);

处理集合

# read all users
$SC->find('user')->read();
# read the users that are marked as verified in a desc order
$SC->find('user')->whereEqual('is_verified', 1)->orderDesc('id')->read();
# read the user with the most reputation
$SC->find('user')->limit(1)->orderDesc('reputation')->readRecord();
# mark users 1 and 3 as verified
$SC->find('user')->whereIn('id', [1, 3])->update(['is_verified' => 1]);
# count the users that don't have a location
$SC->find('user')->whereNull('location')->count();
# plain sql conditions are also supported
$SC->find('user')->where('is_verified = ?', [1])->read();

处理关系

# read the users that have a featured post
$SC->find('user')->has('post')->whereEqual('post.is_featured', 1)->read();
# read the posts of user 1
$SC->find('post')->belongsTo('user')->whereEqual('user.id', 1)->read();
# read the posts that are tagged "php"
$SC->find('post')->hasAndBelongsTo('tag')->whereEqual('tag.name', 'php')->read();
# unconventional FK names are also supported
$SC->find('user')->has('post', 'author_id')->whereEqual('user.id', 1)->read();

执行查询

# read all users
$SC->read('SELECT * FROM user');
# read user 1
$SC->readRecord('SELECT * FROM user WHERE id = ?', [1]);
# read the username of user 1
$SC->readField('SELECT username FROM user WHERE id = ?', [1]);
# read all usernames
$SC->readFields('SELECT username FROM user');
# update all users
$SC->update('UPDATE INTO user SET is_verified = ?', [1]);

注意

  • 关系方法要求表名使用单数形式 - 例如:使用user而不是users
  • 仅与MySQL进行过测试。它可能与Postgres和SQLite兼容,但我还没有测试过。