Jump to:

10928 Posts in 2612 Topics by 1809 members

All other Modules

SilverStripe Forums » All other Modules » Using a URL variable in a dataobject call

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 1601 Views
  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Using a URL variable in a dataobject call Link to this post

    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

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Using a URL variable in a dataobject call Link to this post

    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()

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Using a URL variable in a dataobject call Link to this post

    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

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Using a URL variable in a dataobject call Link to this post

    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

    1601 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.