Editor Primary Key

Editor Primary Key

werbeaccwerbeacc Posts: 7Questions: 2Answers: 0

Hello,

i use the editor extension (1.3.3) and have a problem with the Primary Key.
My Primary Key is "ContentID" and i want to show the Keys in the first column.
The Problem is, that the generated SQL Statement selects two times the field and produce because of that an error:
SELECT ContentID as 'ContentID', ContentID as 'ContentID', ...

Editor::inst( $db, 'xContent')->pkey('ContentID')
    ->fields(
        Field::inst( 'ContentID', 'test' ),

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,382Questions: 1Answers: 10,449 Site admin

    The primary key (regardless of what the column name is in the database) is always called "DT_RowId" in the JSON sent to the client-side (that is one of DataTables' "magic" parameters and instructs it to used the value as the row id. So on the client-side you could do:

    columns: [
      {
        data: "DT_RowId",
        render: function ( data ) {
          return data.replace( 'row_', '' );
        }
      },
      ...
    ]
    

    Note I've used columns.render as well, since the primary key is prefixed by letters to make it a valid DOM id (assuming that your primary key is a number).

    This way you don't need to specify the primary key in a field - Editor will do it automatically for you. The alternative is to alias the field name, but the method above I think is fairly easy to implement.

    Regards,
    Allan

  • werbeaccwerbeacc Posts: 7Questions: 2Answers: 0
    edited October 2014

    Hello,
    thanks for your help.

    I tried your solution, but maybe because of the server side progression (serverSide: true)
    all fields will be POST to the server for paging, sorting...:

    columns[0][data]    DT_RowId
    columns[0][name]    
    columns[0][orderable]   true
    columns[0][search][regex]   false
    columns[0][search][value]   
    columns[0][searchable]  true
    

    and i get the following response:
    {"error":"Unknown field: DT_RowId (index 0)","data":[]}

    I also tried to alias the field name, but it doesent change the SQL Statement, only the JSON output.

    Editor::inst( $db, 'xContent', 'ContentID')
        ->fields(
            Field::inst( 'ContentID as test' ),
    
  • allanallan Posts: 63,382Questions: 1Answers: 10,449 Site admin
    Answer ✓

    Hi,

    Sorry I forgot to reply to this the other day. You are right, server-side processing is going to make that a bit more difficult, I haven't realised you were using it. Unfortunately server-side processing currently makes it impossible without a modification to the PHP libraries.

    Fortunately however, that modification is quite easy :-). If you open Editor.php and find the _ssp_field() function, replace the function completely with:

        public function _ssp_field( $http, $index )
        {
            $name = $http['columns'][$index]['data'];
            $field = $this->_find_field( $name, 'name' );
    
            if ( ! $field ) {
                // Is it the primary key?
                if ( $name === 'DT_RowId' ) {
                    return $this->_pkey;
                }
    
                throw new \Exception('Unknown field: '.$name .' (index '.$index.')');
            }
    
            return $field->dbField();
        }
    

    Then my above solution will work.

    Let me know how you get on with this, but I think I'll probably include it in the next release. I can't see any reason why it shouldn't be since DT_RowId is always the primary key (in these libraries).

    Allan

  • werbeaccwerbeacc Posts: 7Questions: 2Answers: 0

    Hi,

    many thanks, thats work great.
    Hope it will implemented in further releases.

  • allanallan Posts: 63,382Questions: 1Answers: 10,449 Site admin

    Thanks for the feedback! Good to hear that works. Yes, I think I'll include it in the next release.

    Allan

This discussion has been closed.