Last active
August 29, 2015 14:04
-
-
Save olvlvl/e7da6ee382c372cf8f56 to your computer and use it in GitHub Desktop.
issue-activerecord-4
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 ICanBoogie\ActiveRecord; | |
use ICanBoogie\ActiveRecord\Connections; | |
use ICanBoogie\ActiveRecord\Model; | |
use ICanBoogie\ActiveRecord\Models; | |
use ICanBoogie\ActiveRecord\Query; | |
// in the bootstrap of your application | |
require_once __DIR__ . '/vendor/autoload.php'; | |
$connections = new Connections([ | |
'primary' => [ | |
'dsn' => 'sqlite::memory:' | |
] | |
]); | |
$models = new Models($connections,[ | |
'comments' => [ | |
Model::CLASSNAME => 'CommentsModel', | |
Model::ACTIVERECORD_CLASS => 'Comment', | |
Model::SCHEMA => [ | |
'fields' => [ | |
'comment_id' => 'serial', | |
'fk_article_id' => 'foreign', | |
'body' => 'text', | |
'created_at' => 'datetime' | |
] | |
] | |
], | |
'articles' => [ | |
Model::CLASSNAME => 'ArticlesModel', | |
Model::ACTIVERECORD_CLASS => 'Article', | |
Model::HAS_MANY => [ [ 'comments', [ 'foreign_key' => 'fk_article_id' ] ] ], | |
Model::SCHEMA => [ | |
'fields' => [ | |
'article_id' => 'serial', | |
'title' => 'varchar' | |
] | |
] | |
] | |
]); | |
\ICanBoogie\ActiveRecord\Helpers::patch('get_model', function($model_id) use($models) { | |
return $models[$model_id]; | |
}); | |
// this classes you be in their own files | |
class ArticlesModel extends \ICanBoogie\ActiveRecord\Model | |
{ | |
} | |
class Article extends \ICanBoogie\ActiveRecord | |
{ | |
const MODEL_ID = 'articles'; | |
public $article_id; | |
public $title; | |
} | |
class CommentsModel extends \ICanBoogie\ActiveRecord\Model | |
{ | |
protected function scope_ordered(Query $query, $direction=1) | |
{ | |
return $query->order('created_at ' . ($direction < 0 ? 'DESC' : 'ASC')); | |
} | |
} | |
class Comment extends \ICanBoogie\ActiveRecord | |
{ | |
use \ICanBoogie\ActiveRecord\CreatedAtProperty; | |
const MODEL_ID = 'comments'; | |
public $comment_id; | |
public $fk_article_id; | |
public $body; | |
} | |
// test.php | |
$comments = ActiveRecord\get_model('comments'); | |
$articles = ActiveRecord\get_model('articles'); | |
# install and seed articles | |
if (!$articles->is_installed()) | |
{ | |
$articles->install(); | |
for ($i = 1 ; $i < 4 ; $i++) | |
{ | |
$articles->save([ | |
'title' => "Article $i" | |
]); | |
} | |
} | |
# install and seed comments | |
if (!$comments->is_installed()) | |
{ | |
$comments->install(); | |
for ($i = 1 ; $i < 13 ; $i++) | |
{ | |
Comment::from([ | |
'fk_article_id' => ($i - 1) % 3, | |
'body' => "Comment $i", | |
'created_at' => 'now' | |
])->save(); | |
} | |
} | |
var_dump($articles->one->comments->/*filter_by_author("me")->*/ordered->all); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The model Comments now uses
fk_article_id
instead ofarticle_id
to demonstrate how ahas_many
relation can be specified to use foreign key that is not implicit.