Skip to content

Using Template Schema

Yo-An Lin edited this page May 11, 2017 · 4 revisions

You can use Maghead\Schema\TemplateSchema to share the same schema across different table or make them slightly different, here is the sample code:

<?php

namespace App\Model;

use Maghead\Schema\DeclareSchema;
use LazyRecord\Schema\TemplateSchema;

abstract class MetricValueSchema extends TemplateSchema
{
    public function schema() 
    {
        $this->column('site_id')->mediumint()->notNull();

        // The default metric value column
        $this->column('val')->double(5,3)->default(0)->notNull();

        $this->column('unit_id')->int()->notNull();

        $this->column('published_at')->timestamp()->isa('DateTime')->notNull();

        $this->belongsTo('site', 'App\Model\SiteSchema', 'id', 'station_id');

        $this->hasOne('unit', 'App\Model\MetricUnitSchema', 'id', 'unit_id');
    }

    public function provideSchemas()
    {
        $schemas = [];

        $schema = new self;
        $schema->table('clicks');
        $schemas[] = $schema;

        $schema = new self;
        $schema->table('page_views');
        $schemas[] = $schema;

        return $schemas;
    }
}

Clone this wiki locally