Skip to content

Commit 7144164

Browse files
author
ethan
committed
v1.3.0
0 parents  commit 7144164

File tree

15 files changed

+1803
-0
lines changed

15 files changed

+1803
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
/vendor
3+
/.idea
4+
/composer.lock

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# [Database](http://pfinal.cn)
2+
3+
数据库操作类
4+
5+
PHP交流 QQ 群:`16455997`
6+
7+
环境要求:PHP >= 5.3
8+
9+
使用 [composer](https://getcomposer.org/)
10+
11+
```shell
12+
composer require "pfinal/database"
13+
```
14+
15+
```php
16+
<?php
17+
18+
require __DIR__ . '/../vendor/autoload.php';
19+
20+
$config = array(
21+
'dsn' => 'mysql:host=localhost;dbname=test',
22+
'username' => 'root',
23+
'password' => '',
24+
'charset' => 'utf8',
25+
'tablePrefix' => 'db_',
26+
);
27+
28+
$db = new \PFinal\Database\Builder($config);
29+
30+
$id = $db->table('user')->insertGetId(['username' => 'Jack', 'email' => 'jack@gmail.com']);
31+
32+
echo $id;
33+
34+
```
35+
如果你的项目未使用Composer,请使用下面的方式做类自动加载
36+
37+
```php
38+
<?php
39+
40+
include "./src/ClassLoader.php";
41+
42+
$loader = new \PFinal\Database\ClassLoader();
43+
$loader->register();
44+
45+
```

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "pfinal/database",
3+
"description": "PHP MySQL database wrapper, extends PDO and PDOStatement",
4+
"license": "MIT",
5+
"homepage": "http://www.pfinal.cn",
6+
"authors": [
7+
{
8+
"name": "Zou Yiliang"
9+
}
10+
],
11+
"require": {
12+
"php": ">=5.3"
13+
},
14+
"require-dev": {
15+
"phpunit/phpunit": "^5.4"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"PFinal\\Database\\": "src/"
20+
}
21+
}
22+
}

phpunit.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="./tests/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false">
12+
<testsuites>
13+
<testsuite name="Application Test Suite">
14+
<directory>./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
</phpunit>

src/ActiveRecord.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace PFinal\Database;
4+
5+
class ActiveRecord extends Builder
6+
{
7+
public function __construct(array $config = array())
8+
{
9+
parent::__construct($config);
10+
11+
if (static::$modelStorage === null) {
12+
static::$modelStorage = new \SplObjectStorage();
13+
}
14+
$this->table(static::tableName());
15+
}
16+
17+
public static function tableName()
18+
{
19+
$name = get_called_class();
20+
21+
//去掉namespace
22+
$name = rtrim(str_replace('\\', '/', $name), '/\\');
23+
if (($pos = mb_strrpos($name, '/')) !== false) {
24+
$name = mb_substr($name, $pos + 1);
25+
}
26+
27+
//大写转为下划线风格
28+
return trim(strtolower(preg_replace('/[A-Z]/', '_\0', $name)), '_');
29+
}
30+
31+
/**
32+
* @param Connection $db
33+
* @return static
34+
*/
35+
public static function find($db)
36+
{
37+
$model = new static;
38+
return $model->setConnection($db)->asEntity(get_called_class());
39+
}
40+
41+
public function one()
42+
{
43+
return parent::findOne();
44+
}
45+
46+
public function all()
47+
{
48+
return parent::findAll();
49+
}
50+
51+
/**
52+
* @var \SplObjectStorage
53+
*/
54+
protected static $modelStorage = null;
55+
56+
public function findAllBySql($sql = '', $params = array(), $fetchClass = null)
57+
{
58+
if ($fetchClass === null) {
59+
return parent::findAllBySql($sql, $params, $fetchClass);
60+
} else {
61+
62+
$models = parent::findAllBySql($sql, $params, $fetchClass);
63+
64+
array_walk($models, function (&$model) {
65+
static::$modelStorage->attach($model, serialize((array)$model));
66+
});
67+
68+
return $models;
69+
}
70+
}
71+
72+
public function isNewRecord()
73+
{
74+
return !static::$modelStorage->contains($this);
75+
}
76+
77+
/**
78+
* @return bool
79+
*/
80+
public function save()
81+
{
82+
if ($this->isNewRecord()) {
83+
if (($id = $this->insertGetId((array)$this)) > 0) {
84+
$this->id = $id;//todo pk
85+
return true;
86+
}
87+
return false;
88+
} else {
89+
90+
$pk = static::$modelStorage->offsetGet($this);
91+
92+
return 1 == $this->where('id=?', array($pk))->update((array)$this);//todo pk
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)