Tabular record creation

Hi Guys,

Looking for some pointers.

I’m building a basic application with two models. One model is called “Class” and it is a collection of school classes.

Another model is called "Preference" and it contains a teachers preference from 1-4 to teach that unit.

I’m trying to create a form that lists all the classes in the database, and enables the teacher to input their preference (1-4) beside each unit.

Does anyone have any ideas of how to achieve this? I’d prefer if it was based on a gridview or similar.

I’ve looked into Kartik’s Tabular Forms thing, but can’t find a way to make this create missing records (ie, if a preference doesnt exist for that unit?) rather than just update it (plus it needs to show data from multiple tables?)

Cheers

Have you seen this?

http://www.yiiframework.com/doc-2.0/guide-input-tabular-input.html

I did indeed see that, however I have to be honest I wasn’t entirely certain how to use it based around the fact the create/update combined code is missing.

The major issue that I have is that users can’t input a duplicate preference, and I couldn’t see how to avoid that with the above code.

What does your code look like at the moment?

Right now I just have the Gii generated CRUD models and views. I’ve spent my time working on other parts of the system (there’s far more to it than what i’ve just explained in my original post).

I was leaving this till last so I didn’t waste hours approaching it the complete wrong way.

My plan is as follows:

  • I have a class/model named "Preference"

  • The Preference class/model is set up with basic auto-generated relationships to the "teachingperiod" model/class.

  • I want to remove all the current views for the “Preference” class, and replace them with one singular view. That view will need to load in every “teachingperiod”, and put a text box beside it. In that text box, the user enters a preference (1-4). If the user has no preferences in the DB, when they hit submit, it’ll create entries for each teaching period (PeriodID is a foreign key in the Preferences table). If the user already has entries for a unit, it’ll load their previous preference and allow them to update it.

Things get a little complex because TeachingPeriod is related to the "Unit" class/model - because a unit can have multiple teaching periods. i.e. a unit might be offered twice a year (semester 1 and 2, so there is an entry in teaching period for semester 1, and one for semester 2).

Ideally, I was looking at using kartik’s tabular form (I can’t link it here because I haven’t got enough posts).

But i’m struggling to see how to get it to do what I need. All in all, I’m really just needing some direction. I’ve been used to programming in C# ASP.net for some time, so wrapping my head around Yii is a little different.

I’ve never used the tabular form component you mention, so everything I write relates to “plain” Yii2.

From what I understand, you would have a "constant" amount of preferences in your view. Each teacher has the same amount. Technically, if there are ten preferences, they might already have five in the database, making it necessary to create an extra five. Is that correct so far?

Your steps would be:

  • load the teacher’s preferences from the database (have a close look at “indexBy”)

  • create the missing preferences

  • combine both sets of preferences in an array

With that given, you can either send the preferences to the view or load them from post data and save them to the database.