vovanmix / web_orm
基于 PHP 编写的简单轻量级 MySQL ORM
dev-master
2015-06-13 05:49 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: ~4.4.0@stable
This package is not auto-updated.
Last update: 2024-09-14 17:31:58 UTC
README
该项目是一个简单的轻量级 MySQL ORM,使用 PHP 编写。
它支持 PHP 5.3+ 和启用 PDO_MYSQL 的 MySQL。
## 安装
"vovanmix/web_orm": "dev-master"
## 使用方法
要使用它,只需包含类文件并创建一个实例。
require_once("ormPDOClass.php");
// OR
// use Vovanmix\WebOrm\ormPDOClass;
$config = array(
'host' => $HOST,
'base' => $DATABASE_NAME,
'user' => $USER,
'password' => $PASSWORD,
'socket' => $SOCKET //optional
);
$ORM = new ormPDOClass($config);
## 搜索
$foundRecords = $ORM->find('all', 'table1', [
'conditions' => [
['table1.field1', '>' 12],
['table1.field2', '=' [1, 2, 3]],
'OR' => [
['table1.field3', '!=', NULL],
['table1.field4', '.=', 'table1.field5']
]
],
'joins' => [
['table2', [
['table2.field1', '.=', 'table1.field5']
]]
],
'fields' => [
'table2.field1' => 'Name',
'table1.field5' => 'Value'
],
'limit' => 10,
'order' => [
'table1.field1' => 'asc',
'table1.field2' => 'desc'
]
]);
这将执行以下 SQL 查询
SELECT table2.field1 as Name, table1.field5 as Value FROM table1
LEFT JOIN table2 ON table2.field1 = table1.field5
WHERE table1.field1 > 12 AND table1.field2 IN (1, 2, 3) AND (table1.field3 IS NOT NULL OR table1.field4 = table1.field5)
ORDER BY table1.field1 asc, table1.field2 desc
limit 10
你可以使用几种类型的查找查询:`all` 将返回所有找到的记录的数组,`first` 将返回第一个找到的记录,`list` 将以以下格式返回所有记录:`fields` 设置中的第一个值作为键,第二个作为值。
你还可以使用魔术函数来运行查找。
$user = $ORM->getById('users', 1);
//is equal to $ORM->find('first', 'users', ['conditions' => [['id', '=', 1]]]);
$users = $ORM->findByCity('users', 'Los Angeles');
//is equal to $ORM->find('all', 'users', ['conditions' => [['city', '=', 'Los Angeles']]]);
## 其他操作
$ID = $ORM->save('advert_logs', [
'advert_id' => $id,
'price' => $advert['price'],
'time' => date('Y-m-d H:i:s'),
'operation' => $operation,
'comment' => $comment,
'state' => $new_state,
]);
你可以使用对象来插入,它将返回插入记录的 ID。
$updatedCount = $ORM->update('user', [
'last_activity' => date('Y-m-d H:i:s')
],
[
['id', '=', $user_id]
]
);
你可以使用对象来更新,它将返回更新的记录数。
$ORM->remove('variants', [
['user_id', '=', $user_id],
['advert_id', '=', $id],
['demand_id', '=', $current_demand]
]);
你可以使用对象来删除,它将返回删除的记录数。
//include main class and create instance as usual
require_once("model.php");
// OR
// use Vovanmix\WebOrm\Model;
$userModel = new Model( false, 'user', $ORM->connection );
//or
$userModel = new Model( $config, 'user' );
$user = $userModel->getById(1);
## 模型
$ORM->debug = true;
你可以通过省略表名来简化你的代码,使用模型。你需要包含一个模型类并创建一个新实例。
$ORM->print_errors = true;
## 调试
$ORM->fictive = true;
有多个调试参数。