sourcegr / freakquent
An eloquent-like no-bullshit lib
1.0.0
2020-10-04 09:43 UTC
Requires
- sourcegr/query-builder: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2025-05-05 19:34:53 UTC
README
An Eloquent-like, QueryBuilder-like, Doctrine-like, IDE-friently, no-bullshit ORM for PHP.
Setup
<?php use Sourcegr\QueryBuilder\DB; use Sourcegr\Freakquent\Freakquent; use Sourcegr\QueryBuilder\Grammars\MySQL; $grammar = new MySQL([ 'DB' => 'dbname', 'USER' => 'user', 'PASS' => 'passwordd', 'HOST' => '127.0.0.1' ]); $DB = new DB($grammar); Freakquent::init($DB);
Example Contacts Model
<?php namespace Models; use Sourcegr\Freakquent\BaseModel; use Sourcegr\Freakquent\Relations\RelationTrait; class Contact extends BaseModel { use RelationTrait; protected static $table = 'contacts'; public $id; public $name; public $company_id; public function company() { return $this->belongsTo(Company::class, 'company_id', 'id'); } }
Example Company Model
<?php namespace Models; use Sourcegr\Freakquent\BaseModel; use Sourcegr\Freakquent\Relations\RelationTrait; class Company extends BaseModel { use RelationTrait; // set up model's database tablename protected static $table = 'companies'; // set up model's database columns public $id; public $name; // set up a relation. A Company has many contacts public function contacts() { return $this->hasMany(Contact::class, 'id', 'company_id'); } }
How to Use it
Find a model by id
Contact::find(1);
Find and delete it
Contact::find(1).delete();
Find and update it
$c = Contact::find(1); $c->name = 'New name'; $c->save();