Multiple Mjoin with WHERE clause on same tables - How to target different Editor fields

141 Views Asked by At

I have seven (7) similar Mjoins on the same tables, with the only differences being on the WHERE clause, e.g. $q->where( 'discipline_fk', '5' )

Like:

->join(
        Mjoin::inst( 'discipline_outcome' )
            ->name( 'discipline_outcomes_5' )
            ->link( 'unit_outcome.unit_outcome_pk', 'unit_outcome_discipline_outcome_lookup.unit_outcome_fk' )
            ->link( 'discipline_outcome.discipline_outcome_pk', 'unit_outcome_discipline_outcome_lookup.discipline_outcome_fk' )
            ->order( 'discipline_outcome.discipline_outcome asc' )
            ->fields(
                Field::inst( 'discipline_outcome_pk' )
                    ->options( Options::inst()
                        ->table( 'discipline_outcome' )
                        ->value( 'discipline_outcome_pk' )
                        ->label( 'discipline_outcome' )
                        ->where( function ($q) {
                            $q->where( 'discipline_fk', '5' );
                        }
                    ),
                Field::inst( 'discipline_outcome' )
            )
    )
    )

I will need to target these similar Mjoins to different Editor fields. Currently I have just the one for the above Mjoin:

, {
   label: "Discipline Outcome:",
   name: "discipline_outcome[].discipline_outcome_pk",
   type: "select",
   placeholder: 'No selection',
   placeholderDisabled: false,
   placeholderValue: 0,
   multiple: true
                }

How can I setup the 7 Editor fields like above for the 7 Mjoins on the same tables, but with different WHERE clauses, which are distinguished in the Editor fields?

That is, is there another way of targeting which Editor field a join is to send the results to, e.g. in this instance:

name: "discipline_outcome[].discipline_outcome_pk",
1

There are 1 best solutions below

0
On

OK, got it working with using the built-in Editor alias Mjoin->name() method.

Code is:

    ->join(
        Mjoin::inst( 'discipline_outcome' )
            ->name( 'discipline_outcomes_5' )
            ->link( 'unit_outcome.unit_outcome_pk', 'unit_outcome_discipline_outcome_lookup.unit_outcome_fk' )
            ->link( 'discipline_outcomes_5.discipline_outcome_pk', 'unit_outcome_discipline_outcome_lookup.discipline_outcome_fk' )
           // ->order( 'discipline_outcome.discipline_outcome asc' )
            ->fields(
                Field::inst( 'discipline_outcome_pk' )
                    ->options( Options::inst()
                        ->table( 'discipline_outcome' )
                        ->value( 'discipline_outcome_pk' )
                        ->label( 'discipline_outcome' )
                        ->where( function ($q) {
                            $q->where( 'discipline_fk', '4' );
                        }
                    ),
                Field::inst( 'discipline_outcome' )
            )
    )
    )

and in the front end edit:

<editor-field name="discipline_outcomes_5[].discipline_outcome_pk"></editor-field>

and

..., {
                    label: "Discipline Outcome:",
                    name: "discipline_outcomes_5[].discipline_outcome_pk",
                    type: "select",
                    placeholder: 'No selection',
                    placeholderDisabled: false,
                    placeholderValue: 0,
                    multiple: true
                }...