1. Go to this page and download the library: Download visavi/motor-orm library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
visavi / motor-orm example snippets
use MotorORM\Model;
class Article extends Model
{
public string $table = __DIR__ . '/data/articles.csv';
}
$article = Article::query()->find(1);
echo $article->title;
class Article extends Model
{
public string $table = __DIR__ . '/data/articles.csv';
protected array $casts = ['user_id' => 'int', 'views' => 'int'];
public function user(): Relation
{
return $this->hasOne(User::class, 'id', 'user_id');
}
public function scopePublished(Query $query): Query
{
return $query->where('published', 1);
}
}
$articles = Article::query()
->published()
->whereLike('title', '%orm%')
->orderByDesc('created_at')
->paginate(10);
foreach ($articles as $article) {
printf('%s by %s, %d views', $article->title, $article->user->login, $article->views);
}
echo $articles->withPath('/articles')->links();
# By primary key
Article::query()->find(1);
# The first match, or null
Article::query()->where('name', 'Misha')->first();
# Every match as a Collection
Article::query()->where('name', 'Misha')->get();
# Whether anything matches, stops at the first hit
Article::query()->where('name', 'Misha')->exists();
# How many records match
Article::query()->where('created_at', '>', '2009-01-06 08:40:34')->count();
# The column names of the file
Article::query()->headers();
# The record as a plain array
Article::query()->find(1)->toArray();
$article = Article::query()->find(1);
$article->title; // a column
$article->title = 'New title'; // changed in memory
$article->save(); // and written back
$article->update(['text' => 'New text']);
$article->delete();
$article->fresh(); // read again, dropping the unsaved changes
$article->toArray();
Article::query()->find(1); // by halving the file
Article::query()->where('name', 'Bob')->find(1); // by a full walk
# Equality
Article::query()->where('name', 'Misha')->get();
# An explicit operator: = != <> > >= < <=
Article::query()->where('created_at', '>=', '2009-01-06 08:40:35')->get();
# Or
Article::query()->where('id', 1)->orWhere('id', 2)->get();
# In and not in
Article::query()->whereIn('id', [1, 3, 4, 7])->get();
Article::query()->whereNotIn('id', range(1, 10))->get();
# Starts with hi
Article::query()->whereLike('tag', 'hi%')->get();
# Ends with hi
Article::query()->whereLike('tag', '%hi')->get();
# Contains hi
Article::query()->whereLike('tag', '%hi%')->get();
# Exactly hi and nothing more
Article::query()->whereLike('tag', 'hi')->get();
# Everything that does not contain hi
Article::query()->whereNotLike('tag', '%hi%')->get();
# As an alternative to the condition before it
Article::query()->where('id', 1)->orWhereLike('tag', '%hi%')->get();
Article::query()->where('id', 1)->orWhereNotLike('tag', '%hi%')->get();
# Matches NAME, name, namE, Name and so on
User::query()->whereLike('login', 'name')->first();
# Only name
User::query()->whereLike('login', 'name', caseSensitive: true)->first();
# Ascending, the default
Article::query()->orderBy('created_at')->get();
# Descending
Article::query()->orderByDesc('created_at')->get();
Article::query()->orderBy('created_at', SortOrder::Desc)->get();
# Several columns, applied in the order they were added
Article::query()
->orderByDesc('created_at')
->orderBy('id')
->limit(3)
->get();
# Records 11 to 20
Article::query()->offset(10)->limit(10)->get();
foreach (Article::query()->where('active', 1)->cursor() as $article) {
echo $article->title;
}
# Insert, the key is generated when the column is numeric
Article::query()->create(['name' => 'Misha']);
# Insert with an explicit key
Setting::query()->create(['key' => 'theme', 'value' => 'dark']);
# Update every matching record, returns how many were changed
Article::query()->where('name', 'Misha')->update(['text' => 'New text']);
# Update a single record
$article = Article::query()->where('name', 'Misha')->first();
$article->text = 'New text';
$article->save();
# Delete every matching record, returns how many were removed
Article::query()->where('name', 'Misha')->delete();
# Delete a single record
Article::query()->find(17)->delete();
# Remove every record, keeping the column names
Article::query()->truncate();
class Story extends Model
{
protected array $casts = [
'user_id' => 'int',
'views' => 'int',
'rating' => 'int',
'locked' => 'bool',
'meta' => 'array',
];
}
protected array $casts = [
'id' => 'string',
];
class Story extends Model
{
public function scopeActive(Query $query): Query
{
return $query->where('active', true);
}
}
Story::query()->active()->paginate($perPage);
class Story extends Model
{
public function scopeOfType(Query $query, string $type): Query
{
return $query->where('type', $type);
}
}
Story::query()->ofType('new')->paginate($perPage);
# Direct
class User extends Model
{
public function story(): Relation
{
return $this->hasOne(Story::class);
}
}
# Inverse
class Story extends Model
{
public function user(): Relation
{
return $this->hasOne(User::class, 'id', 'user_id');
}
}
class Story extends Model
{
public function comments(): Relation
{
return $this->hasMany(Comment::class);
}
}
class Story extends Model
{
public function tags(): Relation
{
return $this->hasManyThrough(Tag::class, TagStory::class);
}
}
class Story extends Model
{
public function approvedComments(): Relation
{
return $this->hasMany(Comment::class)->constrain(
static fn (Query $query) => $query->where('approved', 1)->orderByDesc('id')
);
}
}
Story::query()->find(1)->lastComment; // one story, one row, its own
Story::query()->with('lastComment')->get(); // three stories, one row between them
$articles = Article::query()->get();
$articles->all(); // the underlying array
$articles->first(); // the first item, or null
$articles->first(fn ($a) => $a->id > 5); // the first match
$articles->last(); // the last item, or null
$articles->count(); // how many items
$articles->isEmpty();
$articles->isNotEmpty();
$articles->get(0, $default); // an item by key
$articles->has(0);
$articles->keys();
$articles->values();
$articles->pluck('title'); // one column as a Collection
$articles->pluck('title', 'id'); // the same, keyed by another column
$articles->keyBy('id'); // the items themselves, keyed by a column
$articles->filter(fn ($a) => $a->id > 5);
$articles->slice(0, 10);
$articles->contains(fn ($a) => $a->id === 3);
$articles->search('needle');
$articles->put('key', $value);
$articles->push($value);
$articles->pull('key'); // remove and return
$articles->forget('key');
$articles->clear();
# Showing 11 to 20 of 45
printf('Showing %d to %d of %d', $articles->firstItem(), $articles->lastItem(), $articles->total());
# null on both when nothing matched
$articles->firstItem();
$articles->lastItem();
# Where the page stands
$articles->onFirstPage();
$articles->onLastPage();
# The url of any page, not only of the ones on show
$articles->url($articles->lastPage());
$migration
// A column text holding "Text" by default, placed after title
->create('text')->default('Text')->after('title')
// A column slug placed before text
->create('slug')->before('text')
->changeTable();
bash
composer bench
# a table and a run count of your own
php benchmarks/bench.php --rows=200000 --runs=5
# only the operations you care about
php benchmarks/bench.php --filter=find