Joomla MVC Component - Updating Database Record With File Upload

949 views Asked by At

So I find this a little weird because the DB field was updating without much effort on my part, until I started writing in file upload functionality. Now the database doesn't seem to be updating, but the file upload is working. Everything seemed to be inserting, updating, etc. just fine simply by specifying proper names in the admin form's XML file:

<?xml version="1.0" encoding="utf-8"?>
<form>
    <fieldset>
        <field
            name="course_id"
            type="hidden" />
        <field
            name="course_name"
            type="text"
            label="Course Name: "
            description="Full name of the course (up to 255 characters)"
            default="" />
        <field
            name="course_dept"
            type="text"
            label="Course Department: "
            description="4 character department code. Examples: BIOL, CREL, CHEM"
            default="" />
        <field
            name="course_code"
            type="text"
            label="Course Code: "
            description="4 digit course code which immediately preceeds department code"
            default="" />
        <field
            name="course_desc"
            type="textarea"
            label="Course Description: "
            rows="10"
            description="As seen in the academic calendar"
            default="" />
        <field
            name="course_graphic_url"
            type="file"
            label="Course Graphic: "
            description="Graphical representation of the course"
            accept="image/*" />
    </fieldset>
</form>

From what I've read, I have to write the database update logic. It's just weird that it seemed to be working before. Now I am new to Joomla MVC component development, so maybe I copy/pasted a few lines of code that would have done it without my realization.. But I doubt it. Here's what I've attempted but I appear to be falling short somewhere. This is /admin/controllers/course.php:

<?php defined('_JEXEC') or die;

jimport('joomla.application.component.controllerform');

class CourseListControllerCourse extends JControllerForm {
protected $view_list = 'CourseList';

function save($key = null, $urlVar = null){
    // ---------------------------- Uploading the file ---------------------
    // Neccesary libraries and variables
    jimport( 'joomla.filesystem.folder' );
    jimport('joomla.filesystem.file');

    $jinput = JFactory::getApplication()->input;
    $files = $jinput->files->get('jform');
    $filename = $files['course_graphic_url']['name'];
    $folder = JPATH_SITE . "/" . "images" . "/" . "courselist";

    // Create the folder if not exists in images folder
    if ( !JFolder::exists( $folder ) ) {
        JFolder::create( $folder, 0777 );
    }

    $src = $files['course_graphic_url']['tmp_name'];
    $dest = JPATH_SITE . "/" . "images" . "/" . "courselist" . "/" . $filename;

    if (isset($files['course_graphic_url'])) {
        JFile::upload( $src, $dest );
    }

    $db = JFactory::getDBO();
    $query = $db->getQuery(true);
    $cid =  $jinput->get('course_id');

    $fields = array($db->quoteName('course_graphic_url') . " = " . $db->quote($dest));
    $conditions = array($db->quoteName('course_id') . " = " . $cid);

    $query
        ->update($db->quoteName('#__courselist'))->set($fields)->where($conditions);

    $db->execute($query);

    return parent::save($key = null, $urlVar = null);
   }
}

Hopefully I've provided enough information.. I'm still stuck in weekend mode so it's hard to say :P Thanks in advance!

EDIT: I realize that the course_id, a url variable that appears when I am in the edit page, was not being passed (as far as I can tell) through when the save button is clicked. I attempted to create a hidden input field with as the value, in hopes that I would fix all that is wrong in the world today but had no luck.

What is strange to me is that every other DB field is updating properly, with no additional SQL needed. The appropriate field was updated with the values in the corresponding form fields. I AM SO CONFUSE RIGHT NOW.

1

There are 1 answers

2
dustin On BEST ANSWER

First of all, big thank you to GDP in Joomla! Stack Exchange for leading me in the right direction. I have to say that the problem was quite a stupid one. As you'll notice in my code, I get a query object:

$query = $db->getQuery(true);

I build the query:

$fields = array($db->quoteName('course_graphic_url') . " = " . $db->quote($dest));
$conditions = array($db->quoteName('course_id') . " = " . $cid);

$query
    ->update($db->quoteName('#__courselist'))->set($fields)->where($conditions);

I, however, do not set the query object!!!! Here is the newly added code. What a dummy :P

$db->setQuery($query);
$db->execute($query);