|
| 1 | +# FlightPHP Active Record |
| 2 | +[](https://packagist.org/packages/flightphp/active-record) |
| 3 | +[](https://packagist.org/packages/flightphp/active-record) |
| 4 | +[](https://packagist.org/packages/flightphp/active-record) |
| 5 | +[](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 |
0 commit comments