5

I'm having strange issue, not sure if I'm doing something wrong. But here is my issue:

I'm working on developing custom helpdesk component. I have custom save method in my Model as follows:

public function save($data, $table)
    {
        $app = JFactory::getApplication();
        $type = 'error';

        if (!$table->bind($data))
        {
            return false;
        }

        if (!$table->check())
        {
            return false;
        }

        try
        {
           $table->store();             
        }
        catch (Exception $ex)
        {
            $app->enqueueMessage($ex->getMessage(), $type);

            return false;
        }

        return $table;
    }

I also have custom function which returns uploaded file information like name, size, type etc. User can upload more than one file that's the reason, I put that information in an Array as follows:

foreach ($filesArr as $file)
{
    $uploads[] = array(
       'hid' => $response->id,
       'name' => $file['name'],
       'size' => $file['size']
     );
}

Output of $uploads[] looks like this:

Array ( 
[0] => Array ( [hid] => 2 [name] => request-for-proprietary-information.pdf [size] => 379984 )
[1] => Array ( [hid] => 2 [name] => vendor-contracting-and-oversight.pdf [size] => 416834 ) )

I went ahead and put it in foreach loop to get each array and then save it in my database.

foreach ($uploads as $upload)
{
     $table = $model->save($upload, $attachment); // Calling save function as mentioned above.
     if ($table === false)
     {
      $app->enqueueMessage(JText::_('COM_HELPDESK_UPLOAD_ERROR'), 'error');
     }
}

Here is structure of my table: Structure of My table where data is supposed to save

Please note that, my Primary key is "ID" which is set on AUTO_INCREMENT. "hid" is not Primary Key.

Above code inserts 1st row in database and then for the rest of loop it just updates the 1st row only.

Query Time: 1.00 ms After last query: 1.00 ms Query memory: 0.009 MB Memory before query: 7.060 MB

INSERT INTO jos_helpdesk_attachment (hid,name,size) VALUES ('2','request-for-proprietary-information.pdf','379984')

Query Time: 2.00 ms After last query: 1.00 msQuery memory: 0.009 MB Memory before query: 7.068 MB

UPDATE jos_helpdesk_attachment SET hid='2',name='vendor-contracting-and-oversight.pdf',size='416834' WHERE id='1'

1
  • I don't think you need to put if(!$table->store()) inside a try clause. Simply do try { $table->store(); }. This isn't the answer to your question, just an off-topic comment ;)
    – Lodder
    Commented Apr 25, 2015 at 19:24

1 Answer 1

2

This is really strange!. I resolved this issue. But I had to explicitly pass "id" column to an array. Hence, I added "id" (primary) key to my existing code:

 foreach ($filesArr as $file)
    {
        $uploads[] = array(
           'id' => '',
           'hid' => $response->id,
           'name' => $file['name'],
           'size' => $file['size']
         );
    }

Hope this will be helpful for others.

1
  • The problem was that JTable updates its internal properties after the first save, which sets the ID to the first record created. So after the first save, it updates that record. What you're doing by sending the empty I'd value is unsetting the internal ID manually. You could also call JTable::reset at the beginning of each loop and it would work. Commented Apr 29, 2015 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.