10376 Posts in 2191 Topics by 1708 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1430 Views |
-
Using a URL variable in a dataobject call

18 September 2009 at 10:06am Last edited: 18 September 2009 10:22am
I have a URL of the form www.blah.co.nz/subscribe?mfid=4
From this I want to send an email with an attachment to the email entered into the form but which attachment, is defined by the variable in the URL, in this case mfid=4. I have sorted out getting the attachment on the email but am now stuck on what I expected to be the easy part - passing URL variable to the dataobject.
even if I remove the data validation $_GET['mfid'] appears not to work. Please don't laugh, I know I must be missing something simple but would really appreciate someones help
if(!preg_match('/^[0-9]{1,2}$/', $_GET['mfid'])){
return;
} else
{
$filename = DataObject::get_by_id("MonkeysFist", $_GET['mfid'])
}
$filepath = "assets/Uploads/mf_attachments/$filename->MFFilename";
$emailToSubmiter->attachFile($filepath, $filename->MFFileName);Thanks
MM
-
Re: Using a URL variable in a dataobject call

18 September 2009 at 3:31pm Last edited: 18 September 2009 3:32pm
Instead of using a preg_match(), you could just cast the URL parameter as an integer. e.g.
$file = DataObject::get_by_id("MonkeysFist", (int) $_GET['mfid']);
if(!$file) return false;
$filepath = "assets/Uploads/mf_attachments/$filename->MFFilename";
$emailToSubmiter->attachFile($filepath, $filename->MFFileName);Are you sure a MonkeysFist record in that table exists with ID 4?
Try adding Debug::show($file) just after the $file =... line to see if the object has actually been returned from DataObject::get_by_id()
-
Re: Using a URL variable in a dataobject call

19 September 2009 at 1:16pm Last edited: 19 September 2009 2:39pm
Hey Sean
Will change to validating the return of the object to simplify things much better way of doing it.
The object is returned, if I run the query outside of sending the email I can get $MFFileName displayed whether I use
$filename = DataObject::get_by_id("MonkeysFist", $_GET['mfid']); or $filename = DataObject::get_by_id("MonkeysFist", 4); and the row is definitely in the tableSo I take that to mean that the problem is not so much with $_GET['mfid'] but what is actually returned, I am wondering about the type.
I have simplified things for testing and am using
$filename = DataObject::get_by_id("MonkeysFist", $_GET['mfid']);
$filepath = "assets/Uploads/mf_attachments/ItsMyLifeCoaching.pdf";
$emailToSubmiter->attachFile($filepath, $filename->MFFileName);as I think there might be a second issue with $filepath which I am running on a separate thread on http://www.silverstripe.org/general-questions/show/268650#post268650.
$filename = DataObject::get_by_id("MonkeysFist", 4);
$filepath = "assets/Uploads/mf_attachments/ItsMyLifeCoaching.pdf";
$emailToSubmiter->attachFile($filepath, $filename->MFFileName);returns an email with an attachment of the name defined by $filename->MFFileName
$filename = DataObject::get_by_id("MonkeysFist", $_GET['mfid']);
$filepath = "assets/Uploads/mf_attachments/ItsMyLifeCoaching.pdf";
$emailToSubmiter->attachFile($filepath, $filename->MFFileName);return and email with an attachment but its name is given as "noname" which I understand as $filename->MFFileName has no value.
I have not been able to get Debug::show($file); to work as I cannot see directly what is coming back. Again I have tried taken it outside of the email function and just done
function Filetest() {
$file = DataObject::get_by_id("MonkeysFist", (int) $_GET['mfid']);
Debug::show($file);
return $file;
}but I don't get anything displayed.
Any thoughts? Thanks
MM
-
Re: Using a URL variable in a dataobject call

19 September 2009 at 5:24pm Last edited: 19 September 2009 5:40pm
If I change the script to
$mfid= isset($_GET['mfid']) ? (int)$_GET['mfid'] : 7;
$fn = DataObject::get_by_id("MonkeysFist", $mfid);
$filepath = "assets/Uploads/mf_attachments/$fn->MFFileName";
$emailToSubmiter->attachFile($filepath, $fn->MFFileName);it works in that I get the attachment for mfid=7 i.e. the reason it is failing is that the script is it is getting no value for mfid. The testing I had done to ensure an object was being returned was using the script when it had been taken out of the surrounding script. Will log a seperate thread as this has now become a very different question. http://www.silverstripe.org/form-questions/show/269304#post269304
Thanks
MM
| 1430 Views | ||
|
Page:
1
|
Go to Top |

