1.5.0 2017-01-10 03:20 UTC

This package is not auto-updated.

Last update: 2024-09-29 00:27:00 UTC


README

TransactdORMPHP 是一个使用 Transactd 插件为 MySQL/MariaDB 提供的 PHP ORM 库。它是 PHP 中最快的 ORM。

描述

  • 高速数据库访问。
  • 模型可以高速访问属性。
  • 可以作为 ActiveRecord 使用。
  • 易于从 Laravel 迁移,接口类似于 Laravel5。
  • 它不依赖于任何框架。也适用于任何 PHP 框架。

执行环境

数据库

客户端

安装

Composer

  • 通过 Composer 安装。
$ cd [yourProjectDirectory]
$ composer require Trnasactd/orm
  • 将以下内容添加到代码开头(如果尚未添加)。
<?php
require __DIR__ . '/vendor/autoload.php'

手册

  1. 从 GitHub 下载。.

  2. 解压到项目目录。

  3. 将以下内容添加到代码开头。

<?php
require __DIR__ . '/TransactdORMPHP-master/src/Require.php'

连接到数据库

主从主机,这些主机可以是同一台主机。如果您指定了不同的主机,则写操作为主机,读操作由从机处理。

Laravel 5.1 以上版本

  • 将以下类添加到您的 config/app.php 服务提供者列表中。
Transactd\boot\Laravel\TransactdLaravelServiceProvider::class,
  • 将以下参数添加到您的 .env 文件中。
// Master and Slave. These are possible to same host.
TRANSACTD_MASTER=tdap://yousername@your_master_host/your_database?&pwd=xxxx
TRANSACTD_SLAVE=tdap://yousername@your_slave_host/your_database?&pwd=xxxx

否则

  • 将以下代码添加到您的应用程序代码开头。
class_alias('Transactd\DatabaseManager', 'DB');
$masterUri = 'tdap://yousername@your_master_host/your_database?&pwd=xxxx';
$slaveUri = 'tdap://yousername@your_slave_host/your_database?&pwd=xxxx';
DB::connect($masterUri, $slaveUri);

使用示例

表名和字段名遵循 ActiveRecord 规则。

<?php
require __DIR__ . '/TransactdORMPHP-master/src/Require.php'

use BizStation\Transactd\Transactd;
use BizStation\Transactd\Database;
use Transactd\Model;

class Group extends Model
{
	public function customers()
	{
		this->hasMany('Customer', 'group')
	}
}

class Customer extends Model
{
	public function group()
	{
		return $this->belongsTo('Group', 'group', 'id');
	}
}

// Connect to databases (Master and Slave. These are possible to same host.)
class_alias('Transactd\DatabaseManager', 'DB');
$masterUri = 'tdap://root@masterhost/test?pwd=xxxx';
$slaveUri = 'tdap://root@slavehost/test?pwd=xxxx';
DB::connect($masterUri, $slaveUri);

// Get all customers
$customers = Customer::all();
echo 'The first customer's id = '. $customers[0]->id;

// Get all customers with group relationship in a snapshot.
DB::beginSnapshot();
$customers = Customer::with('group')->all();
DB::endSnapshot();

// Find a customer id = 1
$customer = Customer::find(1);
echo 'The customer name is '. $customer->name;

// Find customers that group number is 3.
// Transactd query are required that index number and keyValue for start of search.
$customers = Customer::index(1)->keyValue(3)->where('group','=', 3)->get();
echo 'There are '.count($customers).' customers in Group 3.';

// Get a group ralationship from a customer. (belongsTo)
$customer = Customer::find(1);
$group = $customer->group;

// Get customers ralationship from a group. (hasMany)
$group = Group::find(1);
$customers = $group->customers;

// Save a new customer.
$customer = new Customer();
$customer->id = 0;  //autoincrement
$customer->group = 1;
$customer->save();

// Delete a customer in a transaction.
DB::beginTransaction();
$customer->delete();
DB::commit();

// Using native API
$db = DB::master();
$tb = $db->openTable('customrs');
...

文档

  1. 教程
  2. PHP 的 Transactd ORM API
  3. PHP 的 Transactd 客户端 API

错误报告、请求和问题

如果您有任何错误报告、请求或问题,请通过 GitHub 上的问题跟踪器 发送。

许可证

此软件包根据 MIT 许可证授权。