Skip to content

Commit 342beb6

Browse files
committed
Initial commit
0 parents  commit 342beb6

File tree

8 files changed

+1061
-0
lines changed

8 files changed

+1061
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
vendor/
2+
build/
3+
composer.lock
4+
test.db
5+
.vscode/
6+
coverage/
7+
clover.xml
8+
.phpunit.result.cache
9+
.runway-config.json

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 FlightPHP, Lloyd Zhou
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# FlightPHP Active Record
2+
[![Latest Stable Version](http://poser.pugx.org/flightphp/active-record/v)](https://packagist.org/packages/flightphp/active-record)
3+
[![License](https://poser.pugx.org/flightphp/active-record/license)](https://packagist.org/packages/flightphp/active-record)
4+
[![PHP Version Require](http://poser.pugx.org/flightphp/active-record/require/php)](https://packagist.org/packages/flightphp/active-record)
5+
[![Dependencies](http://poser.pugx.org/flightphp/active-record/dependents)](https://packagist.org/packages/flightphp/active-record)
6+
7+
An active record is mapping a database entity to a PHP object. Spoken plainly, if you have a users table in your database, you can "translate" a row in that table to a `User` class and a `$user` object in your codebase. See [basic example](#basic-example).
8+
9+
## Basic Example
10+
11+
Let's assume you have the following table:
12+
13+
```sql
14+
CREATE TABLE users (
15+
id INTEGER PRIMARY KEY,
16+
name TEXT,
17+
password TEXT
18+
);
19+
```
20+
21+
Now you can setup a new class to represent this table:
22+
23+
```php
24+
/**
25+
* An ActiveRecord class is usually singular
26+
*
27+
* It's highly recommended to add the properties of the table as comments here
28+
*
29+
* @property int $id
30+
* @property string $name
31+
* @property string $password
32+
*/
33+
class User extends flight\ActiveRecord {
34+
public function __construct($databaseConnection)
35+
{
36+
parent::__construct($databaseConnection, 'users', [ /* custom values */ ]);
37+
}
38+
}
39+
```
40+
41+
Now watch the magic happen!
42+
43+
```php
44+
// for sqlite
45+
$database_connection = new PDO('sqlite:test.db'); // this is just for example, you'd probably use a real database connection
46+
47+
// for mysql
48+
$database_connection = new PDO('mysql:host=localhost;dbname=test_db&charset=utf8bm4', 'username', 'password');
49+
50+
// or mysqli
51+
$database_connection = new mysqli('localhost', 'username', 'password', 'test_db');
52+
// or mysqli with non-object based creation
53+
$database_connection = mysqli_connect('localhost', 'username', 'password', 'test_db');
54+
55+
$user = new User($database_connection);
56+
$user->name = 'Bobby Tables';
57+
$user->password = password_hash('some cool password');
58+
$user->insert();
59+
// or $user->save();
60+
61+
echo $user->id; // 1
62+
63+
$user->name = 'Joseph Mamma';
64+
$user->password = password_hash('some cool password again!!!');
65+
$user->insert();
66+
67+
echo $user->id; // 2
68+
```
69+
70+
And it was just that easy to add a new user! Now that there is a user row in the database, how do you pull it out?
71+
72+
```php
73+
$user->find(1); // find id = 1 in the database and return it.
74+
echo $user->name; // 'Bobby Tables'
75+
```
76+
77+
And what if you want to find all the users?
78+
79+
```php
80+
$users = $user->findAll();
81+
```
82+
83+
What about with a certain condition?
84+
85+
```php
86+
$users = $user->like('name', '%mamma%')->findAll();
87+
```
88+
89+
See how much fun this is? Let's install it and get started!
90+
91+
## Installation
92+
93+
Simply install with Composer
94+
95+
```php
96+
composer require flightphp/active-record
97+
```
98+
99+
## Documentation
100+
101+
Head over to the [documentation page](https://docs.flightphp.com/awesome-plugins/active-record) to learn more about usage and how cool this thing is! :)
102+
103+
## License
104+
105+
MIT

composer.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "flightphp/session",
3+
"type": "library",
4+
"description": "Tidy session library for Flight PHP",
5+
"keywords": [
6+
"flight",
7+
"session",
8+
"php",
9+
"micro",
10+
"simple",
11+
"easy",
12+
"lightweight",
13+
"framework"
14+
],
15+
"homepage": "https://docs.flightphp.com",
16+
"license": "MIT",
17+
"authors": [
18+
{
19+
"name": "n0nag0n",
20+
"email": "[email protected]",
21+
"role": "Owner"
22+
}
23+
],
24+
"require": {
25+
"php": ">=7.4"
26+
},
27+
"require-dev": {
28+
"phpunit/phpunit": "^9.0",
29+
"squizlabs/php_codesniffer": "^3.8",
30+
"rregeer/phpunit-coverage-check": "^0.3.1"
31+
},
32+
"autoload": {
33+
"psr-4": {"flight\\": "src/"}
34+
},
35+
"scripts": {
36+
"test": "phpunit",
37+
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage --coverage-clover=clover.xml && vendor/bin/coverage-check clover.xml 100",
38+
"beautify": "phpcbf --standard=phpcs.xml",
39+
"phpcs": "phpcs --standard=phpcs.xml"
40+
}
41+
}

phpcs.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ruleset name="pcsg-generated-ruleset">
3+
<description>Created with the PHP Coding Standard Generator. http://edorian.github.io/php-coding-standard-generator/</description>
4+
<arg name="colors"/>
5+
<arg name="tab-width" value="4"/>
6+
<rule ref="PSR12"/>
7+
<!-- <rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/> -->
8+
<file>src/</file>
9+
<file>tests/</file>
10+
</ruleset>

phpunit.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="true" executionOrder="random" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory>src/</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="ActiveRecord Tests">
10+
<directory>tests/</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

0 commit comments

Comments
 (0)