Separate tables CRUD together on one form

EDIT:

Removed bloated comments that serve no purpose.

What I want to do is this:

I have set up 3 tables


persons

	Person ID

	personType <-- Student or Teacher

	title

	firstName

	secondName

	surname

	dateOfBirth

	dateTimeCreated

	

students

	studentID

	personsPersonID <-- foreign key to persons

	healthNotes

	

teachers

	teacherID

	personsPersonID <-- foreign key to persons

	startDate

	formClass

On the front of my site I have 2 buttons, "Create Student" and "Create Teacher"

When you are in "Create Student" you fill out all the columns in persons, AND the Health Notes in students. The app will then create the persons record AND the linked students record.

When you are in "Create Teacher" you fill out all the columns in persons, AND the startDate and formClass in teachers. The app will then create the persons record AND the linked teachers record.

Hope you are with me so far? please let me know if not.

Now, what I was trying to do was use gii to create a model of the persons table with a model class of Students and then add the extra healthNotes column to the model. From that I was going to generate the CRUD code.

See below code for what I mean.

My problems:

  1. I don’t know how to add the healthNotes column to the model so that it’s update-able by the user.

  2. When the user creates a new record using the model below, I need to make sure the student record is also inserted. I am a bit foggy on the best way to do this.

Has anyone ever done this before and has an example code I can look at?


<?php


namespace frontend\models;


use Yii;


/**

 * This is the model class for table "persons".

 *

 * @property integer $personID

 * @property string $personType

 * @property string $title

 * @property string $firstName

 * @property string $secondName

 * @property string $surname

 * @property string $dateOfBirth

 * @property string $dateTimeCreated

 *

 * @property Students $personsPerson

 * @property Students[] $students

 * @property Teachers[] $teachers

 */

class Students extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'persons';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['personType', 'title', 'firstName', 'secondName', 'surname'], 'required'],

            [['personType'], 'string'],

            [['dateOfBirth', 'dateTimeCreated'], 'safe'],

            [['title'], 'string', 'max' => 10],

            [['firstName', 'secondName', 'surname'], 'string', 'max' => 50]

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'personID' => 'Person ID',

            'personType' => 'Person Type',

            'title' => 'Title',

            'firstName' => 'First Name',

            'secondName' => 'Second Name',

            'surname' => 'Surname',

            'dateOfBirth' => 'Date Of Birth',

            'dateTimeCreated' => 'Date Time Created',

        ];

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getPersonsPerson()

    {

        return $this->hasOne(Students::className(), ['personID' => 'personsPersonID']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getStudents()

    {

        return $this->hasMany(Students::className(), ['personsPersonID' => 'personID']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getTeachers()

    {

        return $this->hasMany(Teachers::className(), ['personsPersonID' => 'personID']);

    }

}



Ok, having done even more reading, I can see this is probably not the right way to do this. or it’s just over complicated.

Instead, I am going to put all the fields from persons into both Teachers and Students and just have separate MVC for each.

Then I will create an SQL UNION view on the common fields for the times I want the user to be able to see all the persons together. and point the user to the CRUD of the appropriate table when they want to insert\update or delete.

Hopefully then, I can move forward.

Unless anyone else has a better idea?