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:
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
SEThid
='2',name
='vendor-contracting-and-oversight.pdf',size
='416834' WHEREid
='1'
if(!$table->store())
inside atry
clause. Simply dotry { $table->store(); }
. This isn't the answer to your question, just an off-topic comment ;)