Upload / Filter the files from the metadata tables

Upload / Filter the files from the metadata tables

fabioberettafabioberetta Posts: 74Questions: 23Answers: 4
edited April 2018 in Free community support

Good morning,

I have an issue with upload class in PHP.

I am using it to upload images for a web application. Images are segregated per user and per entity.

When I use ->upload() the web service returns ALL the images recorded into the metadata table. I would like to filter only the relevant images. I did not find any example or snippet.

Essentially I need to add a where clause somewhere.

This is my code.

 Field::inst( 'child_information.document_id' )
            ->setFormatter( 'Format::ifEmpty' , null )
            ->upload( Upload::inst( function ( $file, $id ) {

                    try {

                        $_directory = getenv('DOCUMENT_PHISICAL_PATH') . '/' . $_SESSION['account_id'] .'/';

                        // Move files into dedicated directory
                        move_uploaded_file( $file['tmp_name'], $_directory . $id . '.' . pathinfo( $file['name'], PATHINFO_EXTENSION) );

                    } catch (Exception $e) {
                        echo 'Error: ' . $e->getMessage();
                    }

                    return $id;
                } )
                    ->db( 'document', 'id', array(
                        'original_file_name'    => Upload::DB_FILE_NAME,
                        'file_size'    => Upload::DB_FILE_SIZE,
                        'file_extension' => Upload::DB_EXTN,
                        'type_id' => 'DO',
                        'entity' => 'schedule_document',
                        'account_id' => $_SESSION['account_id']
                    ) )
                    ->validator( function ( $file ) {
                        return$file['size'] >= 15000000
                            ? "Files must be smaller than 15mB"
                            : null;
                    } )
                    ->allowedExtensions( [ 'png', 'jpg', 'pdf', 'doc', 'docx' ], "Please upload an image, a PDF/Word file." )
            ),

Could you help?

Thanks,
Fabio

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    Hi Fabio,

    The Upload method has a where method which you can use to do what you are looking for.

    Allan

  • fabioberettafabioberetta Posts: 74Questions: 23Answers: 4

    Thanks.

    Edited like this and it worked.

     ->db( 'document', 'id', array(
                            'original_file_name'    => Upload::DB_FILE_NAME,
                            'file_size'    => Upload::DB_FILE_SIZE,
                            'file_extension' => Upload::DB_EXTN,
                            'type_id' => 'DO',
                            'entity' => 'schedule_document',
                            'account_id' => $_SESSION['account_id']
                        ))
                        ->where( function ($q) {
                            $q
                                ->where('account_id', $_SESSION['account_id'], '=')
                                ->where('entity', 'schedule_document', '=' );
                        }) 
    
This discussion has been closed.