From b7a2f96cb2475df797f5c9fa78a0d360c836d6ac Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sun, 16 Oct 2016 23:30:54 +0100 Subject: [PATCH] comment support --- src/Models/Comment.php | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Models/Post.php | 10 ++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/Models/Comment.php diff --git a/src/Models/Comment.php b/src/Models/Comment.php new file mode 100644 index 0000000..1dff841 --- /dev/null +++ b/src/Models/Comment.php @@ -0,0 +1,54 @@ + + */ +class Comment extends Model +{ + protected $table = DB_PREFIX . 'comments'; + protected $primaryKey = 'comment_ID'; + protected $dates = ['comment_date', 'comment_date_gmt']; + public $timestamps = false; + + /** + * Create a new Eloquent model instance. + * + * @param array $attributes + * @return void + */ + public function __construct(array $attributes = []) + { + parent::__construct($attributes); + + // Set the current table to the site's own table if we're in a multisite + if (Wordpress::multisite() && (Wordpress::getSiteId() !== 0 && Wordpress::getSiteId() !== 1)) { + $this->setTable(DB_PREFIX . Wordpress::getSiteId() . '_comments'); + } + } + + /** + * Get the post that this comment belongs to. + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function post() + { + return $this->belongsTo(Post::class, 'comment_post_ID'); + } + + /** + * Get the user that posted this comment. + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function user() + { + return $this->belongsTo(User::class); + } +} diff --git a/src/Models/Post.php b/src/Models/Post.php index c24e1a5..314a4ac 100644 --- a/src/Models/Post.php +++ b/src/Models/Post.php @@ -86,6 +86,16 @@ class Post extends Model } /** + * Get all the comments that belong to this post. + * + * @return HasMany + */ + public function comments() + { + return $this->hasMany(Comment::class, 'comment_post_ID'); + } + + /** * Get meta values for this Post. * * @param array|string|null $key -- libgit2 1.7.2