Upload file with additional column

Upload file with additional column

newrednewred Posts: 12Questions: 7Answers: 0
edited November 2016 in Free community support

My "files" table column are TargetId,File_Name, File_Size, Web_Path. I can upload file and store the record and file with no issue. But i don't know how to pass additional info(TargetId) to the editor php file to insert into TargetId column in "file" table

My client side

                       $(document).ready(function() {
                         editor = new $.fn.dataTable.Editor( {
                                //  ajax: "{{ asset('/Include/staff.php') }}",
                                 ajax: { 'type': 'POST', 'data': {"userid": 4}, 'url':  "{{ asset('/Include/staff.php') }}"},
                                 table: "#stafftable",
                                 idSrc: "users.Id",
                                 fields: [
                                          {
                                                  label: "Image:",
                                                  name: "files.Web_Path",
                                                  type: "upload",
                                                  display: function ( file_id ) {
                                                      return '<img src=""/>';
                                                  },
                                                  clearText: "Clear",
                                                  noImageText: 'No image'
                                          },{
                                                 label: "Name:",
                                                 name: "users.Name"
                                         }, {
                                                 label: "Email:",
                                                 name: "users.Email"
                                         }, {
                                                 label: "Contact No:",
                                                 name: "users.Contact_No"
                                         }, {
                                                 label: "Address:",
                                                 name: "users.Address"
                                         }, {
                                                 label: "User_Type:",
                                                 name: "users.User_Type",
                                                 type: "hidden",
                                                 default: "Staff"
                                         }, {
                                                 label: "DOB:",
                                                 name: "users.DOB"
                                         }, {
                                                 label: "IC No:",
                                                 name: "users.IC_No"
                                         }, {
                                                 label: "Gender:",
                                                 name: "users.Gender"
                                         }, {
                                                 label: "Marital Status:",
                                                 name: "users.Marital_Status"
                                         }, {
                                                 label: "Position:",
                                                 name: "users.Position"
                                         }, {
                                                 label: "Emergency Contact Person:",
                                                 name: "users.Emergency_Contact_Person"
                                         }, {
                                                 label: "Emergency Contact No:",
                                                 name: "users.Emergency_Contact_No"
                                         }, {
                                                 label: "Emergency Contact Relationship:",
                                                 name: "users.Emergency_Contact_Relationship"
                                         }, {
                                                 label: "Emergency Contact Address:",
                                                 name: "users.Emergency_Contact_Address"
                                         }

                                 ]
                         } );

while my server side i wanted to upload and insert the TargetId

<?php

include( "plugin/php/DataTables.php" );

// Alias Editor classes so they are easy to use
use
  DataTables\Editor,
  DataTables\Editor\Field,
  DataTables\Editor\Format,
  DataTables\Editor\Mjoin,
  DataTables\Editor\Upload,
  DataTables\Editor\Validate;
/*
 * Example PHP implementation used for the index.html example
 */

// DataTables PHP library


  // Build our Editor instance and process the data coming from _POST
  $editor=Editor::inst( $db, 'users' )
    ->fields(
      Field::inst( 'users.Id' ),
        Field::inst( 'users.Name' )->validator( 'Validate::notEmpty' ),
      Field::inst( 'users.Email' ),
      Field::inst( 'users.Contact_No' ),
      Field::inst( 'users.Address' ),
      Field::inst( 'users.User_Type' ),
      Field::inst( 'users.DOB' ),
      Field::inst( 'users.IC_No' ),
      Field::inst( 'users.Gender' ),
      Field::inst( 'users.Marital_Status' ),
      Field::inst( 'users.Position' ),
      Field::inst( 'users.Emergency_Contact_Person' ),
      Field::inst( 'users.Emergency_Contact_No' ),
      Field::inst( 'users.Emergency_Contact_Relationship' ),
      Field::inst( 'users.Emergency_Contact_Address' ),
      Field::inst( 'files.Web_Path' )
            ->setFormatter( 'Format::ifEmpty', null )
            ->upload( Upload::inst( realpath(__DIR__ . '/..').'/private/upload/__ID__.__EXTN__' )
                ->db( 'files', 'Id', array(
                    'Type'    => 'User',
                    'TargetId'    => $_POST['TargetId'],
                    'File_Name'    => Upload::DB_FILE_NAME,
                    'File_Size'    => Upload::DB_FILE_SIZE,
                    'Web_Path'    => Upload::DB_WEB_PATH
                ) )
                ->validator( function ( $file ) {
                    return$file['size'] >= 5000000 ?
                        "Files must be smaller than 5000K" :
                        null;
                } )
                ->allowedExtensions( array( 'png', 'jpg', 'gif' ), "Please upload an image" )
            )
    )
    ->leftJoin('files', 'files.TargetId', '=', 'users.Id')
    ->process( $_POST )
    ->json();

Kindly advise how to pass the current edited value and a file to upload and insert into "file" table. thanks. Urgent

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

Answers

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

    I think I see the issue - are you trying to have the users.Id value inside the files table (as TargetId)? The Editor upload uses the inverse of that - the users table should reference the files table!

    I would expect your left join to be:

     ->leftJoin('files', 'files.Id', '=', 'users.FileId')
    

    You'd need to add the FileId (or similar) to the users table.

    I'm afraid the Editor upload class doesn't operate the way you currently have it setup.

    Allan

This discussion has been closed.