How to get the 'system_path' value after uploadMany

How to get the 'system_path' value after uploadMany

brendonsbrendons Posts: 39Questions: 14Answers: 1
edited July 2016 in Free community support

Hi All,
I am using the php libraries. I have followed the uploadMany example at "/examples/advanced/upload-many.html" and it works as I want.
After the form is submitted I am running a function via the ->on('postCreate') method:

        ->on( 'postCreate', function ( $editor, $id, $values, $row ) {
            qEmails( $editor->db(), $id, $values, $row );
        } )

The $row array contains a nested array of the "id"(s) of my uploaded file(s), eg: "files":[{"id":"2"},{"id":"3"}]}],
I now need to get the 'system_path' value which is held in the files table.
Is the 'system_path' value available already or do I need to query the table?

I've tried:

        function qEmails ( $db, $id, $values, $row ) {
            $resultset = array();
            for ($x=0; $x < count($row["files"]); $x++) {
                $y = $row["files"][$x]["id"];
                $results = $db->raw()
                    ->bind(':enrid', $y)
                    ->exec("SELECT system_path FROM files WHERE id = :enrid");
                foreach($results['data'] as $result) {  //not sure of this syntax
                $resultset[] = $result['system_path'];  //not sure of this syntax
                }
        }
        $qAttachments = json_encode($resultset);
        ...
        }

without success. Any help would be appreciated.

One more point of discussion:
When I return the data for a single user record from uploadMany.php it contains a huge list of every file that has been uploaded in connection with this table. The documentation doesn't really explain how this is useful. Is it not a performance hit to return so much 'un-requested' data from a table with many records?

Brendon

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Replies

  • allanallan Posts: 62,301Questions: 1Answers: 10,216 Site admin

    Hi Brendon,

    Is the 'system_path' value available already or do I need to query the table?

    Assuming you are using the Upload->db() method then it should be in the $row parameter. Exactly where will depend upon your configuration. If you do file_put_contents( '/tmp/editor-upload', print_r( $row, true ) ); it will dump the $row parameter into the file specified so you could have a look at the data structure to know where to get that data.

    it contains a huge list of every file that has been uploaded in connection with this table. The documentation doesn't really explain how this is useful

    It isn't. Its almost but not quite a bug (since it is working as I currently designed it - the bug is in the design of that aspect!). This will change with 1.6 which will be released later this summer.

    Allan

  • brendonsbrendons Posts: 39Questions: 14Answers: 1

    Thanks for the reply Allan.

    I am using the uploadMany example as given. I think it uses the upload->db method:

    ->join(
            Mjoin::inst( 'files' )
                ->link( 'users.id', 'users_files.user_id' )
                ->link( 'files.id', 'users_files.file_id' )
                ->fields(
                    Field::inst( 'id' )
                        ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/Editor156/examples/advanced/upload/__ID__.__EXTN__' )
                            ->db( 'files', 'id', array(
                                'filename'    => Upload::DB_FILE_NAME,
                                'filesize'    => Upload::DB_FILE_SIZE,
                                'web_path'    => Upload::DB_WEB_PATH,
                                'system_path' => Upload::DB_SYSTEM_PATH
                            ) )
    

    I tried printing out the $row var but unfortunately it only contains the id from the files table.

    Array
    (
        [DT_RowId] => row_32
        [users] => Array
            (
                [first_name] => Alexa
                [last_name] => Wilder
                [phone] => 1-727-307-1997
                [site] => 2
            )
        [sites] => Array
            (
                [name] => London
            )
        [files] => Array
            (
                [0] => Array
                    (
                        [id] => 6
                    )
            )
    )
    

    I also printed out the $values array but the 'system_path' is not there either.

    In my previous post, do I have the right syntax to handle the results of $db->raw() ?

    Brendon

  • allanallan Posts: 62,301Questions: 1Answers: 10,216 Site admin

    Ah - yes, sorry, I was mistaken this morning (I was thinking about left joins). You are correct that you would need to query the database based on the id to get the meta information about the file.

    In my previous post, do I have the right syntax to handle the results of $db->raw() ?

    Yes, but you need to get the results:

    $res = $db->raw()
                ->bind(':enrid', $y)
                ->exec("SELECT system_path FROM files WHERE id = :enrid");
    
    $rows = $res->fetchAll();
    ...
    

    You could also use the select method rather than raw().

    Allan

  • brendonsbrendons Posts: 39Questions: 14Answers: 1

    Many thanks Allan.

This discussion has been closed.