CrispinP
Well-known member
Hi folks,
In an add-on I'm working on I have to manually attach a file to a resource.
I had it working by manually copying to the file to the data directory, and inserting the various bits into the xf_attachment and xf_attachment_data tables but thought that was a bit messy.
So, I picked apart the importer code and now have this:
I get back data_id and newAttachmentId but they don't exist in the DB.
Is there an uncommited transaction I need to commit?
is there a "global" save I need to call?
thanks
Crispin
In an add-on I'm working on I have to manually attach a file to a resource.
I had it working by manually copying to the file to the data directory, and inserting the various bits into the xf_attachment and xf_attachment_data tables but thought that was a bit messy.
So, I picked apart the importer code and now have this:
Code:
$temporaryImageDirectory = XenForo_Helper_File::getTempDir();
$basename = basename($sourceFile);
// make sure basename fits 100 character limit
if (strlen($basename) > 100)
{
$strlen = strlen($basename);
$pos_start = $strlen - 100;
$basename = substr($basename,$pos_start);
}
$tempFullPath = tempnam(XenForo_Helper_File::getTempDir(), 'xf'). "." . $ext;
//doing other stuff here, code removed
$img->writeImage($tempFullPath);
//here a valid image file exists and can be opened in any image viewer.
$filex = new XenForo_Upload("preview1.jpg", $tempFullPath);
//print_r($filex);
$uploadDate = time();
$att = $this->getModelFromCache('XenForo_Model_Attachment');
$dataExtra = array('upload_date' => $uploadDate, 'attach_count' => 1);
$data_id = $att->insertUploadedAttachmentData($filex, $currentUserId, $dataExtra);
$dw = XenForo_DataWriter::create('XenForo_DataWriter_Attachment');
$dw->setImportMode(true); //not yet sure if I should have this in or not.
$dw->bulkSet(array(
'data_id' => $data_id,
'content_type' => 'resource_update',
'content_id' => $resourceId,
'attach_date' => $uploadDate,
'unassociated' => 0
));
$dw->save();
$newAttachmentId = $dw->get('attachment_id');
print "newAttachmentId:" . $newAttachmentId;
I get back data_id and newAttachmentId but they don't exist in the DB.
Is there an uncommited transaction I need to commit?
is there a "global" save I need to call?
thanks
Crispin