Quantcast
Channel: Recent Discussions — DataTables forums
Viewing all articles
Browse latest Browse all 82694

Again: SELECT DISTINCT

$
0
0

@allan, SELECT DISTINCT has been discussed multiple times in various threads. Recently in this one: https://datatables.net/forums/discussion/55145/select-distinct#latest

I think there are situations in which you really need this. I have a fairly complex join in this Editor instance. And no matter what I do - and I really tried everything. It's not possible to specify the join and / or the where clause in a way that the results are unique. My work around is the "PHP SELECT DISTINCT" in the "postGet" event handler. It's ok, it works and I can live with it.

I think it would be great if Editor could do a SELECT DISTINCT. Why would it hurt for editing? I mean you have the row id ?!

Here is my code which makes something from my change log legible for my users. This code works but it will produce multiple identical rows without the '"postGet" solution.

Editor::inst( $db, 'log' )
    ->field(
        Field::inst( 'log.id' )->set( false ),
        Field::inst( 'log.user_id AS log.changer' )->set( false )
            ->getFormatter( function($val, $data, $opts) {
                return getUserName($val);
            }), 
        Field::inst( 'log.values AS log.affected_user' )->set( false )
            ->getFormatter( function($val, $data, $opts) {
                $row = json_decode($val, true);
                return getUserName($row["userId"]);
            }),     
        Field::inst( 'log.action' )->set( false ),
        Field::inst( 'log.values AS log.user_role' )->set( false )
            ->getFormatter( function($val, $data, $opts) {
                $row = json_decode($val, true);
                return $row["ctr_govdept_has_user"]["role"];
            }), 
        Field::inst( 'log.values AS log.user_dept' )->set( false )
            ->getFormatter( function($val, $data, $opts) {
                $row = json_decode($val, true);
                $govNameStr = '';
                foreach ($row["gov"] as $gn) {
                    $govNameStr .= ( $gn["govName"] . ', ' );
                }
                $govNameStr = substr($govNameStr, 0, -2);
                return $row["ctr_govdept"]["dept_name"] . ' (' . 
                       $govNameStr . ')';
            }),                 
        Field::inst( 'log.when as log.update_time' )->set( false )
    )
    ->leftJoin( 'ctr_govdept_has_user_complete', 'log.user_id', '=', 'ctr_govdept_has_user_complete.user_id' )
    ->leftJoin( 'ctr_govdept_has_ctr_installation', 'ctr_govdept_has_user_complete.ctr_govdept_id', '=', 'ctr_govdept_has_ctr_installation.ctr_govdept_id' )
    ->leftJoin( 'ctr_installation_has_principal', 'ctr_govdept_has_ctr_installation.ctr_installation_id', '=', 'ctr_installation_has_principal.ctr_installation_id' )
    ->where( function($q) {
        $q ->where( 'ctr_installation_has_principal.user_id', $_SESSION['id'] );
        $q ->where( 'log.table', 'ctr_govdept_has_user' );
    })
    ->on( 'postGet', function ( $e, &$data, $id ){ 
       $data = array_values(array_unique($data, SORT_REGULAR));
    })
    ->process($_POST)
    ->json();

Viewing all articles
Browse latest Browse all 82694

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>