在 Laravel 中将模型实例与其自己的 Class 相关联的好方法是什么?

What is a good way of associating a Model instance with it's own Class in Laravel?

我确信这种事情有一个共同的模式,我正在努力寻找搜索词来寻找答案,所以如果这是一个骗局,请耐心等待。

我的应用程序中有一些 类 创建非常标准的模型,这些模型存储在关系数据库中,例如;

// AtsType::name examples = 'XML', 'RSS', 'SOAP'

class AtsType extends Model
{
    public function ats_instances()
    {
        return $this->hasMany('App\AtsInstance');
    }

    public function import()
    {

    }
}

但是,我需要 import() 方法执行的操作是根据实际模型实例以某种方式调用 class/interface/contract/whatever。所以像这样;

AtsTypeRss::import() AtsTypeXml::import() AtsTypeSoap::import()

我希望它们是独立的 类,以便最终使用一些 artisan 命令为开发人员生成它们,以及数据迁移以创建新模型名称进入数据库。

我只是不确定该怎么做。

您可以尝试类似的方法(如 所示),我搜索了如何在命名空间中使用变量:

class AtsType extends Model
{
    protected $import_method = 'MyMethod';

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

    public function import()
    {
        $string = $this->import_method;
        $class = '\controller\' . $string;
        $newObject = new $class();
    }
}