Schema
I have the following database schema:
=== modules ===
id: PK
name: String
current_revision: FK to revisions.id
=== revisions ===
id: PK
module_id: unsigned int, indexed, FK to modules.id
parent_revision: unsigned int, nullable, indexed, FK to revisions.id
content: string
Example Data
Example data
modules:
(1, "Maths, 3)
(2, "Computing", 5)
revisions:
(1, 1, null, "Maths - v1")
(2, 1, 1, "Maths- v2")
(3, 1, 2, "Maths - v3")
(4, 2, null, "Computing - v1")
(5, 2, 4, "Computing - v2")
Explanation
As you can see, the parent_revision
relates to the previous version of that module or null
if it's the first version for the module.
The current_revision
relates to the latest version for the category
What I want?
I wish to represent this relationship as a model in Laravel. I managed to make a start:
class Module extends Model
{
public function currentRevision()
{
return $this->belongsTo(Revision::class, 'current_revision_id');
}
public function revisions()
{
/* Help! (I wish to return all the revisions for this module in
order, starting from the current_revision. e.g. for the "Maths"
module it
should return the revisions with IDs: [3, 2, 1] and for the
"Computing" module it should return: [4, 3] )*/
}
}
-
class Revision extends Model
{
public function module()
{
return $this->belongsTo(Module::class);
}
public function nextRevision()
{
return $this->hasOne(Revision::class, 'parent_revision');
}
public function previousRevision()
{
return $this->belongsTo(Revision::class, 'parent_revision');
}
public function previousRevisions()
{
// TODO: return ALL previous revisions
}
}
I wish to find an efficient way to create the revisions()
method for the Module
model. How can I do this?
NOTE: I don't mind if you suggest schema changes, provided it is better than what I have currently!
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire