Duplicating code

My project is a Veterinary system and I have several models including

=> registration - register farmers/semen suppliers


=> dairy - register animals served and include farmers ID (registartion model) 		=> semen - captures animal served(dairy model) plus the animals owner (registration model)

My problem is duplicating code for instance in semen model i derive the farmers name from registration and the dairy animal served from dairy.

How is this accomplished proffesional to enhance efficiency. eg this code is duplicated in both dairy and semen models

public function getRegistration()


{


	return $this->hasOne(Registration::className(), ['user_id' => 'user_id']);


}	

DB Schemas

CREATE TABLE registration (

firstname varchar(30) DEFAULT NULL,

lastname varchar(30) DEFAULT NULL,

email varchar(30) DEFAULT NULL,

id_no int(20) DEFAULT NULL,

gender varchar(1) DEFAULT NULL,

type varchar(1) DEFAULT NULL COMMENT ‘farmer, semen supplier (1,2)’,

user_id int(10) unsigned NOT NULL AUTO_INCREMENT,

PRIMARY KEY (user_id)

)

CREATE TABLE dairies (

type int(3) NOT NULL COMMENT ‘freshian, holstein etc’,

milk_quantity int(2) DEFAULT NULL,

dairy_name varchar(20) DEFAULT NULL,

id int(10) unsigned NOT NULL AUTO_INCREMENT,

user_id int(10) DEFAULT NULL COMMENT ‘registration table user_id’,

PRIMARY KEY (id),

UNIQUE KEY type (type)

)

CREATE TABLE semen (

name varchar(20) DEFAULT NULL,

type varchar(20) DEFAULT NULL,

user_id int(10) DEFAULT NULL,

code varchar(20) DEFAULT NULL,

id int(10) unsigned NOT NULL AUTO_INCREMENT,

PRIMARY KEY (id)

)

Not every code that’s the same is a duplicate.