Can I establishing order (on server-side) when use fileupload ?

Can I establishing order (on server-side) when use fileupload ?

aneto2400aneto2400 Posts: 58Questions: 8Answers: 1

Hi Allan,

When used fileuplod, you get files (on AJAX call) that come in order by ID on table (asc orden i think). Can be change this order in server side or in ajax call?. I´m working in standalone, that means that i not use table , therefore i can't to use methods that apply on table, but I need to establishing an order on base of the field ('es_un`).

If there are any way to establishing this order on fileuplod (on server side) please tell me know.

Any idea?

Thank you in advance!
Eduardo

  • Related server-side code -
...
Editor::inst( $db, 'chef_recomd' )
    ->fields(
        Field::inst( 'site' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'id_lang' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'nombre' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'condimentos' ),
        Field::inst( 'es_un' )->validator( 'Validate::notEmpty' ), 
        Field::inst( 'alergeno' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'precio' )->validator( 'Validate::numeric' ),
        Field::inst( 'prec_terraza' )->validator( 'Validate::numeric' ),
        Field::inst( 'nota_item' ),
        Field::inst( 'image' )
            
            ->setFormatter( 'Format::nullEmpty' )



            ->upload( 
                Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/upload/__ID__.__EXTN__' )

                ->db( 'files_chef_recomd', 'id', array(
                    'site'          =>$site,
                    'filename'    => Upload::DB_FILE_NAME,
                    'filesize'    => Upload::DB_FILE_SIZE,
                    'web_path_thumb1' => '', 
                    'sys_path_thumb1' => '', 
                ) )
                
                 // for upload file
                ->where( function ( $q ) use ( $site ) {
                    $q->where( 'site', $site );
                    
                  } )

            ),

...

    ->where( 'site', $site)
            
    ->process( $_POST )
    ->json();

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    Currently no - there isn't an option for that in the libraries. However, you can easily add one in the Upload.php file. Specifically in the data public method there is:

            $q = $db
                ->query( 'select' )
                ->table( $this->_dbTable )
                ->get( $this->_dbPKey );
    

    You can add ->order( ... ) into that chain to add an order by clause to the SQL statement.

    Allan

  • aneto2400aneto2400 Posts: 58Questions: 8Answers: 1

    Hi Allan,
    I´ve to add line (see below) but it to do nothing, what I doing wrong?

    Also, I understand that the field must to belong to file table, but I could refer a field of another table? (field that not on file table)

    Thank you!
    Eduardo

        public function data ( $db, $id=null )
        {
            if ( ! $this->_dbTable ) {
                return null;
            }
    
            // Select the details requested, for the columns requested
            
            $q = $db
                ->query( 'select' )
                ->table( $this->_dbTable )
                ->get( $this->_dbPKey )
                ->order('filename DESC'); // add this line
    
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    That looks like all that should be needed. Perhaps enable the debug mode by adding ->debug( true ) just before the ->process(...) method in your main Editor PHP configuration. That will show the SQL that is being executed for each request in the returned JSON.

    Also, I understand that the field must to belong to file table, but I could refer a field of another table? (field that not on file table)

    If you make the query into a Join, then yes, you could do that.

    Allan

  • aneto2400aneto2400 Posts: 58Questions: 8Answers: 1
    edited October 2017

    Hi Allan,

    Congrats for your API and your help, each day I learn more about it.

    Debug and my opinion : it look that take order() and is included on SQL sentence (see below), but the JSON output is not the wished ( is the same). I means, with order() JSON output should be 845, 843, 846, 844 (diferent output).. right?

    NOTE: order used on this debug:
    ->order('filesize ASC');

    Thank you!
    Eduardo

    without order():
    
    debugSql:
    SELECT  `id` as 'id', `site` as 'site', `filename` as 'filename', `filesize` as 'filesize', `web_path_thumb1` as 'web_path_thumb1', `sys_path_thumb1` as 'sys_path_thumb1' FROM  `files_chef_recomd` WHERE (`site` = :where_1 )"
    
    files:
    843:{id: "843", site: "342800010", filename: "LC demo paella.jpg", filesize: "77078",…}
    844:{id: "844", site: "342800010", filename: "LC demo pescado.jpg", filesize: "114215",…}
    845:{id: "845", site: "342800010", filename: "grilled-1631727_640.jpg", filesize: "70264",…}
    846:{id: "846", site: "342800010", filename: "quark-2114712_640.jpg", filesize: "77117",…}
    
    
    with  order():
    
    debugSql:
    SELECT  `id` as 'id', `site` as 'site', `filename` as 'filename', `filesize` as 'filesize', `web_path_thumb1` as 'web_path_thumb1', `sys_path_thumb1` as 'sys_path_thumb1' FROM  `files_chef_recomd` WHERE (`site` = :where_1 ) ORDER BY `filesize`  ASC "
    
    files:
    843:{id: "843", site: "342800010", filename: "LC demo paella.jpg", filesize: "77078",…}
    844:{id: "844", site: "342800010", filename: "LC demo pescado.jpg", filesize: "114215",…}
    845:{id: "845", site: "342800010", filename: "grilled-1631727_640.jpg", filesize: "70264",…}
    846:{id: "846", site: "342800010", filename: "quark-2114712_640.jpg", filesize: "77117",…}
    
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    Ah yes - the trick here is that the file information is in an object, not an array. And objects inherently have no order (looking at the one above, it looks like whatever serialised it for output ordered by key).

    What I think you'd actually need to do is take the files object and use $.map or similar to convert it to be an array and the sort it as required. The ->order() method on the server-side isn't actually going to be of any help in this case. Sorry about that!

    Allan

  • aneto2400aneto2400 Posts: 58Questions: 8Answers: 1

    Hi Allan,

    None problem, always is very interesting your comments.
    Ok, now I understand.

    Then, only on data [] ( array on JSON output) can be apply order() on server-side?. The current API has this method "order() on data[]" or also I must be add?

    Thank you!
    Eduardo

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    Then, only on data [] ( array on JSON output) can be apply order() on server-side?

    That is correct. Although to a large extent, that is actually irrelevant itself since DataTables will sort the data in the data array itself.

    To get ordered file information you could use ajax.json() to get the JSON object returned by the server, then use $.map() on its files object to convert the objects into arrays and sort them. Its a bit of a pain, but that's the only way I see of doing that.

    Thanks,
    Allan

  • aneto2400aneto2400 Posts: 58Questions: 8Answers: 1

    Thank you for help me to resolve this question.

    Greetings,
    Eduardo

This discussion has been closed.