0 votes

Is it possible to make a Base Model class for each Module?

So in BaseModel get:

use App\MyModule\MyModuleModel;

abstract class generated_class extends MyModuleModel

The reason is that I want possibility to override $connection for a complete module, so it fetches data from a second database.

/Frode

in How To & Manuals by (300 points)
edited by

Hi,

I'm not sure if such an implementation would help you. Because Abstract models are erased and generated on every export, so you would not be able to edit/insert anything into abstract classes.

What is the common way how is this solved without Skipper?

Ludek

The common way would be to just extend another class.
Why not have it as a Module property?
then it can be set on export

Ok, can you please post more details about your original idea? I guess I misunderstood you original post.

Please send full source code(s) with marked what and how you would like to have exported. And also where/what ORM properties you want to add in Skipper to achieve it.

Thanks

Here is modified sample. Used the default models when opening a new Laravel project:

app/Models/AbstractModels/AbstractSampleEntity.php:

<?php
/**
 * Model object generated by: Skipper (http://www.skipper18.com)
 * Do not modify this file manually.
 */

namespace App\Models\AbstractModels;
namespace App\Models;

abstract class AbstractSampleEntity extends AbstractBaseModel
{
}

Here you can see instead of using the Eloquent Model, we use a new AbstractBaseModel.

app/Models/AbstractBaseModel.php:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

abstract class AbstractBaseModel extends Model
{
    /**
     * The name of the "created at" column.
     *
     * @var string
     */
    const CREATED_AT = 'created';

    /**
     * The name of the "updated at" column.
     *
     * @var string
     */
    const UPDATED_AT = 'modified';

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $connection = 'my_custom_database_override';
}

This is now a global basemodel, but I would have liked that you . could have one per module, but you should be able to set this as property somehow.

Do you see the benefit of this? I have 100 Models imported from a second database. I do not want my laravel app to have its base tables in this database so I have 2 connections. This works fine, but I have to add the $connection varialbe manually on all models.. this could easily be added by generator.

Thanks for details.

Unfortunately, this is the problem I mentioned before.

Abstract classes live in app/Models/AbstractModels directory, so if we want to extends all existing AbstractEntity* from AbstractBaseModel we have to put this class also to directory app/Models/AbstractModels/...

So, the file AbstractBaseModel have to be located in app/Models/AbstractModels/abstractBaseModel.php

But, as I wrote before, all files in AbstractModels directory are automatically erased and generated on every export. So there is no way how to allow users to edit it. And it's not a good idea to store AbstractBaseModel in the parent directory.

Also, as you mentioned in your example, you're now trying to add more properties than $connection to AbstractBaseModel (createdat and updatedat). But there is no way how to edit/display such settings in Skipper.

You're trying to simulate some property-inheritance or traits via class inheritance. I believe there are better tools to achieve that. For example mentioned traits, where you add such trait to derived classes where Skipper will not touch it.

Please log in or register to answer this question.