Relationship design

I am creating an application in which clients can create a document from a predefined template, edit some fields with their own text and save it. I have outlined the relationships, as I think they will, and basically it’s normal to convert to Laravel:

enter image description here

The only question I have is how I will handle FieldValue relationships. The idea is that the Template defines all the fields, and then instead of re-creating them on each document, it should just look at its template for them. This would mean that FieldValue should look at its Document, at the Template for this and find the appropriate field from there.

Is there a clean way to implement this, or is there a better way to develop a relationship to make it more practical to implement?

+4
3

...

, Laravel :

class Document extends Model
{
    public function template()
    {
        return $this->belongsTo('App\Template');
    }

    public function fields()
    {
        return $this->belongsToMany('App\Field')->withPivot('value');
    }
}

class Template extends Model
{
    public function organisation()
    {
        return $this->belongsTo('App\Organisation');
    }

    public function documents()
    {
        return $this->hasMany('App\Document');
    }

    public function fields()
    {
        return $this->hasManyThrough('App\Field', 'App\Section');
    }

    public function sections()
    {
        return $this->hasMany('App\Section');
    }
}

class Section extends Model
{
    public function fields()
    {
        return $this->hasMany('App\Document')->withPivot('value');
    }

    public function template()
    {
        return $this->belongsTo('App\Template');
    }
}

class Field extends Model
{
    public function documents()
    {
        return $this->belongsToMany('App\Document')->withPivot('value');
    }

    public function section()
    {
        return $this->belongsTo('App\Section');
    }
}

class Organisation extends Model
{
    public function documents()
    {
        return $this->hasManyThrough('App\Document', 'App\Template');
    }

    public function templates()
    {
        return $this->hasMany('App\Template');
    }
}

( laravel ):

fields
    id - integer
    section_id - integer

documents
    id - integer
    template_id - integer

templates
    id - integer
    organisation_id - integer

sections
    id - integer
    template_id - integer

organisations
    id - integer

document_field
    id - integer
    document_id - integer
    field_id - integer
    value - string

. :

$user = App\User::find(3);

$organisation = $user->organisation;

foreach ($organisation->documents as $document)
{
    foreach ($document->fields as $field)
    {
        echo $field->pivot->value;
    }
}

:

$field = App\Field::find(2);

$document = App\Document::find(4);

$value = 'My field value';

$document->fields()->save($field, ['value' => $value]);

:

+1

, , :

$doc = Document::findOrFail(Input::get('docId'));
$sections = $doc->template->sections;
$fieldValues = $doc->field values;

.

fieldValue

->with('field');
+1

:

. , . . .

:

ERD ERD:

.

  • , .
  • . , . . , .
  • - . /.
  • . . . . content_id . , , .
  • , , - FieldValue/Content. . .

, . , :

. . ! , . , !

+1
source

All Articles