Upload many - store id

Upload many - store id

paintitblackpaintitblack Posts: 60Questions: 20Answers: 0
edited March 2018 in Editor

Hi,

is it possible to store the user id directly in the file ** table without using an extra **users_files (https://editor.datatables.net/examples/advanced/upload-many.html )

I mean like this:

->join(
         Mjoin::inst( 'files' )
             ->fields(
                 Field::inst( 'id' )
                     ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/upload/__ID__.__EXTN__' )
                         ->db( 'files', 'id', array(
                             'user_id'    => 'users.id',
                             'filename'    => Upload::DB_FILE_NAME,
                             'filesize'    => Upload::DB_FILE_SIZE,
                             'web_path'    => Upload::DB_WEB_PATH,
                             'system_path' => Upload::DB_SYSTEM_PATH
                         ) )
                         ->validator( Validate::fileSize( 500000, 'Files must be smaller that 500K' ) )
                         ->validator( Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif' ), "Please upload an image" ) )
                     )
             )
     )
     ->leftJoin( 'sites', 'sites.id', '=', 'users.site' )

I removed the ->link two times and added the user_id in the db area.

How can I submit the user id to the db area?

Is it possble to get a solution only with the users and files table (without an extra link with the users_files table)?

Thanks in advance

This question has an accepted answers - jump to answer

Answers

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

    No - I'm afraid not. The user row (assuming we are talking about the examples here) might not have been created before the file was uploaded (which happens before the form is submitted).

    If however you have the user id as a session variable and you are meaning the user that is doing the upload, then yes, that is possible. Use something like:

    'user_id' => function () {
      return $_SESSION['user_id'];
    }
    

    Allan

  • paintitblackpaintitblack Posts: 60Questions: 20Answers: 0

    ok obviously I have to create the users_files table :-)

    I tried to set up a plain version and combine it with the parent / child exampe (https://datatables.net/blog/2016-03-25), but now I get the error "DataTables warning: table id=sites - Table part of the field "id" was not found. In Editor instances that use a join, all fields must have the database table set explicitly." but I have the field "id" in each table (see at db.txt).

    Here is my site.php snippet:

    Editor::inst( $db, 'sites' )
        ->fields(
            Field::inst( 'id' )->set( false ),
            Field::inst( 'name' )->validator( 'Validate::notEmpty' )
        )
        ->join(
            Mjoin::inst( 'users' )
                ->link( 'sites.id', 'users.site' )
                ->fields(
                    Field::inst( 'id' )
                )
        )
        ->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'].'/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
                            ) )
                            ->validator( Validate::fileSize( 500000, 'Files must be smaller that 500K' ) )
                            ->validator( Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif' ), "Please upload an image" ) )
                        )
                )
        )
        ->leftJoin( 'sites', 'sites.id', '=', 'users.site' )
    
        ->process( $_POST )
        ->json();
    

    Where is the mistake? Please can you help me again?

    Kind regards

    pib

  • paintitblackpaintitblack Posts: 60Questions: 20Answers: 0

    I solved the issue, but I stoll don't get an entry in the users_files table.

    This code works pretty well except the entry in the users_files table:

        Editor::inst( $db, 'users' )
            ->field(
                Field::inst( 'users.first_name' ),
                Field::inst( 'users.last_name' ),
                Field::inst( 'users.phone' ),
                Field::inst( 'users.site' )
                    ->options( 'sites', 'id', 'name' )
                    ->validator( 'Validate::dbValues' ),
                Field::inst( 'sites.name' )
            )
            ->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( '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
                                     ) )
                                     ->validator( Validate::fileSize( 500000, 'Files must be smaller that 500K' ) )
                                     ->validator( Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif' ), "Please upload an image" ) )
                                 )
                         )
                 )
            ->leftJoin( 'sites', 'sites.id', '=', 'users.site' )
            ->where( 'site', $_POST['site'] )
            ->process($_POST)
            ->json();
    

    What should I add?

    Thanks in advance?

    pib

  • paintitblackpaintitblack Posts: 60Questions: 20Answers: 0

    ok I always have to press the "update" button :-)

    Is there any possibility to create the "users_files" entry directly without pressing the "update" button?

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

    I'm afraid not. The form needs to be submitted for that write to happen. Consider for example the create case - the user id wouldn't exist until the row is interested into the database.

    Regards,
    Allan

  • paintitblackpaintitblack Posts: 60Questions: 20Answers: 0

    ok thanks.

    The file would be uploaded and the files entry get creates before the user presses the "Update" button, but who cares. I am fine with this solution :-)

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

    Yes, that's the way it works at the moment. This has come up a few times recently, so I will look into providing an synchronous option for the upload in future.

    Allan

This discussion has been closed.