searching MJoin Association table in reverse

searching MJoin Association table in reverse

lowrymellowrymel Posts: 20Questions: 4Answers: 0

Hi All,

I am successfully building Association tables based on the example 'Join tables - one-to-many join' [https://editor.datatables.net/examples/advanced/joinArray.html ] for many different use cases.

The example does a great job of creating association between users and permissions from the user perspective.
Is it possible to use the same association table and search it from the permission perspective. In otherwords, locate all users that have the permission 'Accounts'? This would provide the reverse data view using the same association tables and would make a great audit.

It seems possible but I can not get it to work.

Any help is greatly appreciated.

Thanks,

Michael

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Hi Michael,

    Yes, that should be perfectly possible. I haven't actually run the code, but this should do it:

    Editor::inst( $db, 'permission' )
        ->field(
              Field::inst( 'permission.name' )
        )
        ->join(
            Mjoin::inst( 'permission' )
                ->link( 'users.id', 'user_permission.user_id' )
                ->link( 'permission.id', 'user_permission.permission_id' )
                ->order( 'last_name asc' )
                ->fields(
                    Field::inst( 'first_name' ),
                    Field::inst( 'last_name' ),
                    Field::inst( 'id' )
                        ->validator( Validate::required() )
                        ->options( Options::inst()
                            ->table( 'permission' )
                            ->value( 'id' )
                            ->label( 'name' )
                        ),
                )
        )
        ->process($_POST)
        ->json();
    

    Allan

  • lowrymellowrymel Posts: 20Questions: 4Answers: 0

    That worked.
    Many thanks Allan.

This discussion has been closed.