Skip to content

Instantly share code, notes, and snippets.

@andersonfraga
Last active April 1, 2020 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersonfraga/1166400 to your computer and use it in GitHub Desktop.
Save andersonfraga/1166400 to your computer and use it in GitHub Desktop.
Spell : IdentityMap, ORM...
<?php
namespace Spell\Entity\Author;
use Spell\Column\Serial as Serial,
Spell\Column\Str as Str;
class Author {
public $id, $name, $uri;
function __construct() {
$this->id = Serial::readonly();
$this->name = Str::len(120)->is_need();
$this->uri = Str::len(80);
}
};
<?php
use Spell\Entity\Post as Post
Spell\Expression\Create as Expression,
Spell\Expression\Create\Transaction as Transaction;
/*
SELECT * FROM Post WHERE title = 'Abra cadabra'
*/
$post = Expression::build(new Post)->filter(function($obj) {
$obj->title->eq("Abra cadabra");
});
/*
SELECT * FROM Post WHERE title != 'Abra cadabra'
*/
$post = Expression::build(new Post)->filter(function($obj) {
$obj->title->diff("Abra cadabra");
});
/*
SELECT id, author, title, text FROM Post WHERE title != 'Abra cadabra'
*/
$post = Expression::build(new Post)->filter(function($obj) {
$obj->title->diff("Abra cadabra");
})->fields('id', 'author', 'title', 'text');
/*
SELECT * FROM Post WHERE datetime <= '2011-03-22' LIMIT 12
*/
$post = Expression::build(new Post)->filter(function($obj) {
$obj->datetime->lteq(date('Y-m-d'));
})->limit(12);
/*
SELECT * FROM Post WHERE datetime <= '2011-03-22' LIMIT 10 OFFSET 12
*/
$post = Expression::build(new Post)->filter(function($obj) {
$obj->datetime->lteq(date('Y-m-d'));
})->limit(12, 10);
/*
SELECT * FROM Post WHERE (datetime BETWEEN '2011-03-17' AND '2011-03-22' OR author = 12) LIMIT 10 OFFSET 12
*/
$post = Expression::build(new Post)->filter(function($obj) {
$obj->clausure_or(function($and_of) {
$and_of->datetime->between(date('Y-m-d', strototime('-5 days')), date('Y-m-d'));
$and_of->author->eq(12); // Implicito $and_of->author->id->eq(12);
});
})->limit(12, 10);
/*
SELECT * FROM Post WHERE (datetime BETWEEN '2011-03-17' AND '2011-03-22' AND author = 12) OR author = 10 LIMIT 10 OFFSET 12
*/
$post = Expression::build(new Post)->filter(function($obj) {
$obj->clausure_or(function($or_filter) {
$or_filter->clausure_and(function($and_of) {
$and_of->datetime->between(date('Y-m-d', strototime('-5 days')), date('Y-m-d'));
$and_of->author->eq(12); // Implicito $and_of->author->id->eq(12);
});
$or_filter->author->eq(1o);
});
})->limit(12, 10);
/*
SELECT *
FROM Post P, Author A
WHERE P.datetime > '2011-03-22'
AND P.author = A.id
AND A.name LIKE '%Anderson%'
LIMIT 10 OFFSET 12
*/
$post = Expression::build(new Post)->filter(function($obj) {
$obj->datetime->rt(date('Y-m-d'));
$obj->author->name->likelr('Anderson');
})->limit(12, 10);
/*
// No MySQL, equivalente à:
SET autocommit = 0;
START TRANSACTION;
INSERT INTO author (name, email) VALUES ('Abc132', 'Abc132@def.com');
ID_AUTHOR = LAST_INSERT_ID();
INSERT INTO post (author, title, datetime, text) VALUES (ID_AUTHOR, 'Lalalalala', 123456789, 'blablablablablabla');
COMMIT();
SET autocommit = 1;
*/
$transaction = function() {
$author = new Author;
$author->name = "Abc132";
$author->email = "Abc132@def.com";
$post = new Post;
$post->author = Expression::save($author);
$post->title = "Lalalalala";
$post->datetime = time();
$post->text = "blablablablablabla";
Expression::save($post);
};
Transaction::create($transaction);
// Pode-se 'guardar' para posterior execução da transação também! ;)
$binding = Transaction::bind($transaction);
// Executando...
$binding->execute();
<?php
namespace Spell\Entity\Post;
use Spell\Column\Serial as Serial,
Spell\Column\Number as Number,
Spell\Column\Date\Time as Datetime,
Spell\Column\Str as Str;
class Post {
public $id, $author, $title, $datetime, $text;
function __construct() {
$this->id = Serial::readonly();
$this->author = Number::is_need()->has_many('Author::id');
$this->title = Str::len(120)->is_need();
$this->datetime = Datetime::is_need();
$this->text = Str::is_need()->name('texto' /* nome diferente no BD */);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment