Andi1982's avatar

fixed file to database: extend laravel model vs. using a trait

I get from a mainframe system a fixed file and have to store its data into the mysql database. The problem is that there are different kind of record types. So not each record looks same. Here is some example data:

0001:081212345:K01:  :CUSTOMER_FIRSTNAME       :CUSTOMER_LASTNAME        :278:0000:ANYSTRING
0002:081212345:K04:  :OTHER_DATA   :00012983,93:00073823,72:     :EUR
0003:081212345:K08:J1:HEADER_NAME :      :ADDITIONAL_DATA      :FREE_TEXT
0003:081212345:K10:J1:FIELD1            :FIELD2                  :FIELD3
0003:081212345:PAW:J1:WORK_DESCRIPTION            :FREE_TEXT             :COSTS
0003:081212345:PET:J1:PART_NUMBER         :PRICE       :CURRENCY
0003:081212345:K08:J2:HEADER_NAME :      :ADDITIONAL_DATA      :FREE_TEXT
0003:081212345:K10:J2:FIELD1            :FIELD2                  :FIELD3
0003:081212345:PAW:J2:WORK_DESCRIPTION            :FREE_TEXT             :COSTS

Each field is separated by a colon. First field is just the line number, then some order-number and the third field is the record type. Now I am reading the file line by line and would make a table and model for each of the record-types.

Now I am thinking what is the best way to fill the model with the data. I have some test class with functions to get the fields for each record type from the record string:

class K01
{

	public $data = '';

	public __construct($data) 
	{
		$this->data = $data;
	}

	/**
	 * Array of Fields [record_field_name, Offset, Length, db-fieldname]
	 */
	protected array $fieldConfig = [
		'ID' => [0,  4, ''],
		'ORDER_NO' => [5,  9, 'order_no'],
		'RECORD_TYPE' => [15,  3, 'record_type'],
		'FIRSTNAME' => [22, 25, 'firstname'],
		'LASTNAME' => [45, 25, 'lastname'],
		'AREA_CODE' => [74, 3, 'area_code'],
		'AGE' => [78, 4, 'age'],
		'ANYSTRING' => [83, 9, 'anystring'],
	];


	public function getField($fieldName)
	{
		if (! isset($this->fieldConfig[$fieldName])) {
			return '';
		}

		list($off, $len, $dbName) = $this->fieldConfig[$fieldName];

		return substr($data, $off, $len);

	}

}

The best way would be if I could have the K01-model and could fill it like that:

$k01 = new \App\models\K01($recordString);

But it would need that I manipulate the construction logic of the laravel model. So also ok would be a static function like:

$k01 = \App\models\K01::fillFromRecordString($recordString);

And now my dilemma. There are at all 14 different record-types. So I will need 14 different models and each of them should be able to handle this logic.

I never before used own traits, normally I extended the classes. What would be the best way to get my fill-model-method into the classes? Would you do it by writing a troit or better write a MainframeModel class and extend with it the laravel-model class?

class MainframeModel extends model
{
   static function fillFromRecordString($data)
   {
        // logic
   }

}
class K01 extends MainframeModel 
{
}

or better

trait MainframeTrait
{
   static function fillFromRecordString($data)
   {
        // logic
   }
}

class K01 extends model
{
    use MainframeTrait;
}

I am really curious how experienced people here would implement this thing to get the records into the database.

best regards Andi

0 likes
3 replies
Andi1982's avatar

@vincent15000 The colon is not always there and also can appear in the data fields. And each record looks different. So I need my own routines to read it.

1 like
vincent15000's avatar

@Andi1982 Ok I understand.

I would rather create a MainframeModel and then extend each class with it.

Please or to participate in this conversation.