Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Micro Active Record library in PHP, supports chain calls, events, and relations.

License

NotificationsYou must be signed in to change notification settings

flightphp/active-record

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Latest Stable VersionLicensePHP Version RequireDependencies

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 aUser class and a$user object in your codebase. Seebasic example.

Basic Example

Let's assume you have the following table:

CREATETABLEusers (  idINTEGERPRIMARY KEY,   nameTEXT,   passwordTEXT );

Now you can setup a new class to represent this table:

/** * An ActiveRecord class is usually singular * * It's highly recommended to add the properties of the table as comments here * * @property int    $id * @property string $name * @property string $password */class Userextendsflight\ActiveRecord {publicfunction__construct($databaseConnection)  {parent::__construct($databaseConnection,'users', [/* custom values */]);  }}

Now watch the magic happen!

// for sqlite$database_connection =newPDO('sqlite:test.db');// this is just for example, you'd probably use a real database connection// for mysql$database_connection =newPDO('mysql:host=localhost;dbname=test_db&charset=utf8bm4','username','password');// or mysqli$database_connection =newmysqli('localhost','username','password','test_db');// or mysqli with non-object based creation$database_connection =mysqli_connect('localhost','username','password','test_db');$user =newUser($database_connection);$user->name ='Bobby Tables';$user->password =password_hash('some cool password');$user->insert();// or $user->save();echo$user->id;// 1$user->name ='Joseph Mamma';$user->password =password_hash('some cool password again!!!');$user->insert();echo$user->id;// 2

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?

$user->find(1);// find id = 1 in the database and return it.echo$user->name;// 'Bobby Tables'

And what if you want to find all the users?

$users =$user->findAll();

What about with a certain condition?

$users =$user->like('name','%mamma%')->findAll();

See how much fun this is? Let's install it and get started!

Installation

Simply install with Composer

composerrequire flightphp/active-record

Documentation

Head over to thedocumentation page to learn more about usage and how cool this thing is! :)

License

MIT

About

Micro Active Record library in PHP, supports chain calls, events, and relations.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors6

Languages


[8]ページ先頭

©2009-2025 Movatter.jp