Skip to content

Instantly share code, notes, and snippets.

@tanakahisateru
Created December 15, 2015 13:34
Show Gist options
  • Select an option

  • Save tanakahisateru/dba2da4c9fab088f2d5a to your computer and use it in GitHub Desktop.

Select an option

Save tanakahisateru/dba2da4c9fab088f2d5a to your computer and use it in GitHub Desktop.
Eloquent bidirectional reference test
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\Article
*
* @property integer $id
* @property integer $blog_id
* @property string $title
* @property string $body
*/
class Article extends Model
{
public $timestamps = false;
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function comments()
{
return $this->hasMany(Comment::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function blog()
{
return $this->belongsTo(Blog::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\Blog
*
* @property integer $id
* @property string $name
*/
class Blog extends Model
{
public $timestamps = false;
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function articles()
{
return $this->hasMany(Article::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\Comment
*
* @property integer $id
* @property integer $article_id
* @property string $body
*/
class Comment extends Model
{
public $timestamps = false;
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function article()
{
return $this->belongsTo(Article::class);
}
}
<?php
namespace App\Console\Commands;
use App\Models\Article;
use App\Models\Blog;
use App\Models\Comment;
use Illuminate\Console\Command;
class OrmTest extends Command
{
protected $signature = 'ormtest';
protected $description = 'Command description';
public function handle()
{
\DB::disableQueryLog();
$blogId = $this->up();
\DB::enableQueryLog();
$this->info("Case 1");
$blogs = Blog::with('articles', 'articles.comments')->get();
foreach($blogs[0]->articles[0]->comments as $comment) {
echo $this->formatComment($comment);
}
foreach (\DB::getQueryLog() as $log) {
$this->comment(sprintf("%s: %s", $log['time'], $log['query']));
}
\DB::flushQueryLog();
$this->info("Case 2");
$blogs = Blog::with('articles', 'articles.comments')->get();
foreach($blogs[0]->articles[0]->comments as $comment) {
echo $this->formatComment2($blogs[0], $blogs[0]->articles[0], $comment);
}
foreach (\DB::getQueryLog() as $log) {
$this->comment(sprintf("%s: %s", $log['time'], $log['query']));
}
$this->down();
}
/**
* @param Comment $comment
* @return string
*/
private function formatComment(Comment $comment)
{
return sprintf("%s - %s: %s\n",
$comment->article->blog->name,
$comment->article->title,
$comment->body
);
}
/**
* @param Blog $blog
* @param Article $article
* @param Comment $comment
* @return string
*/
private function formatComment2(Blog $blog, Article $article, Comment $comment)
{
return sprintf("%s - %s: %s\n",
$blog->name,
$article->title,
$comment->body
);
}
/**
* @return int
*/
private function up()
{
$blog = new Blog();
$blog->name = 'hoge';
$blog->save();
foreach (range(1, 5) as $i) {
$article = new Article();
$article->blog()->associate($blog);
$article->title = $i;
$article->body = "Loren " . $i;
$article->save();
foreach (range(1, 10) as $j) {
$comment = new Comment();
$comment->article()->associate($article);
$comment->body = $i . $j;
$comment->save();
}
}
return $blog->id;
}
/**
* @throws \Exception
*/
private function down()
{
foreach (Comment::all() as $comment) {
/** @var Comment $comment */
$comment->delete();
}
foreach (Article::all() as $article) {
/** @var Article $article */
$article->delete();
}
foreach (Blog::all() as $blog) {
/** @var Blog $blog */
$blog->delete();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment