S
S
Slavanb2016-10-21 13:31:33
Yii
Slavanb, 2016-10-21 13:31:33

How to create one ActiveRecord model based on another ActiveRecord model in Yii2?

How to dynamically pull the properties of one model into another?
There is a main ActiveRecord model called HUMAN ('People') , it contains data such as full name, contact details and so on.
class Human extends ActiveRecord
For convenience, let's assume that it has only 3 properties ( ID, NAME, CONTACT)
And there are models of teachers ( TEACHER ) and students ( PUPIL ) that extend the properties of the HUMAN model, namely, they contain properties specific either only for teachers, or only for students, respectively.
And it is declared like this:
class Teacher extends Human
For convenience, suppose that TEACHER has only 3 property fields, which are taken from the table of the same name, namely (ID ,HUMAN_ID, PREDMET)
ID - ID in the teacher table
HUMAN_ID - a link to the ID in the table HUMAN
PREDMET - the name of the subject taught by this teacher
Thus, I can say I combine 2 models into one for convenience, and the distribution logic for tables is implemented in methods such as BeforeSave.
However, in order for this scheme to work, you have to force the properties of the HUMAN model in the TEACHER model, for some reason they are not inherited. (in the TEACHER table I prescribe public $NAME; public CONTACT;) The
question is how to make the properties of the HUMAN model inherited or transferred to the TEACHER model, without forced enumeration, because if you expand the properties of the HUMAN model, you will have to make changes to TEACHER.
TEACHER model code:
<?php
namespace common\models;
use Yii;
class Teacher extends Human
{
// How to pull these properties DYNAMICLY from the HUMAN model and why aren't they inherited?
// ----------------------------
public $NAME;
public $CONTACT;
// ----------------------------
public static function tableName()
{
return 'TEACHER';
}
public function rules()
{
return [
[['HUMAN_ID'], 'integer'],
[['PREDMET'], 'string'],
[['HUMAN_ID'], 'exist', 'skipOnError' => true , 'targetClass' => Human::className(), 'targetAttribute' =>
[['NAME'], 'required'],
[['NAME'], 'string', 'max' => 200],
[['CONTACT'], 'string', 'max' => 200],
];
}
public function attributeLabels()
{
return [
'ID' => Yii::t('app', 'ID'),
'HUMAN_ID' => Yii::t('app', 'Human ID'),
'PREDMET ' => Yii::t('app', 'Predmet'),
'NAME' => Yii::t('app', 'Name'),
'CONTACT' => Yii::t('app', 'CONTACT'),
];
}
public function getHuman0()
{
return $this->hasOne(Human::className(), ['ID' => 'HUMAN_ID']);
}
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if($this->isNewRecord){
// if we are creating a new user, then we need to create
// a record for him in the profile table with a reference to the parent table
$human = new Human;
$human->NAME = $this->NAME;
$ok=$human->save();
$this->HUMAN_ID=$human->ID;
if (!$ok) return false;
} else
{
if (($human = Human::findOne($this->HUMAN_ID)) !== null) {
$human->NAME=$this->NAME;
$ok=$human->save();
if (!$ok) return false; }
}
return true;
}
return false;
}
public function afterDelete()
{
parent::afterDelete();
if (($human = Human::findOne($this->HUMAN_ID)) !== null) {
$ok= $human->delete();
if (!$ok) return false; }
return true;
}
}
Model HUMAN :
<?php
namespace common\models;
use Yii;
class Human extends \yii\db\ActiveRecord
public static function tableName()
{
return 'HUMAN';
}
public function rules()
{
return [
[[ 'NAME'], 'required'],
[['NAME', 'CONTACT'], 'string', 'max' => 200],
];
}
public function attributeLabels()
{
return [
'ID' => Yii::t('app', 'ID'),
'NAME' => Yii::t('app', 'Name'),
'CONTACT' => Yii::t('app', 'Contact'),
];
}
}

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Fedorov, 2016-10-21
@qonand

this is what you made up ... You have absolutely the wrong concept. Firstly, why do you need the basic Human model? Judging by its meaning, this is generally a redundant class. But even if we assume that it is needed for some reason, then judging by the structure of the database, your data is organized in the form of relations, and you work with them through inheritance.
Do not look for the answer to your question - as you are now going in the wrong direction. Better read the documentation in Russian and in particular this

M
Maxim Timofeev, 2016-10-21
@webinar

Two tables, two models data access through relations
What for to fence still something?
If, apart from teachers, there are still types of people, create more models, and in the PEOPLE table you make an additional field "type" and in the PEOPLE model the getData function, which returns data from one or another connection depending on the type.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question