Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Using a URL variable in a dataobject call


Reply


4 Posts   1693 Views

Avatar
mschiefmaker

Community Member, 187 Posts

18 September 2009 at 10:06am

Edited: 18/09/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

Avatar
Sean

Forum Moderator, 922 Posts

18 September 2009 at 3:31pm

Edited: 18/09/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()

Avatar
mschiefmaker

Community Member, 187 Posts

19 September 2009 at 1:16pm

Edited: 19/09/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 table

So 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

Avatar
mschiefmaker

Community Member, 187 Posts

19 September 2009 at 5:24pm

Edited: 19/09/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