Last active
April 1, 2020 12:17
-
-
Save andersonfraga/1166400 to your computer and use it in GitHub Desktop.
Spell : IdentityMap, ORM...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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', '[email protected]'); | |
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 = "[email protected]"; | |
$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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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