Jump to:

23459 Posts in 18906 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Accessing Fields of a has_one relation

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1 2
Go to End
Author Topic: 2278 Views
  • Lukin
    Avatar
    Community Member
    42 Posts

    Accessing Fields of a has_one relation Link to this post

    Hi
    I saving an dataobject 'userbrille' via a frontend form.

    The dataobject includes a has_one relation to file.

    After saving a new 'userbrille' I want to access the file wich is an image
    to resize it. But I don't know who to do it.

    the object:

       static $db = array (
          'Name' => 'Text',
          'Email' => 'Text'
       );

       static $has_one = array (
          'Bild' => 'File'
       );


    saving a new UserBrille:

    $neuesBild = new UserBrille();
    $neuesBild->setField('Name',$data->getVar('Name'));
    $neuesBild->setField('Email',$data->getVar('Email'));
    $neuesBild->setField('BildID',$data->getVar('BildID'));
    $neuesBild->write();
    //return $neuesBild->renderWith(array('UserBrillePageCrop'));

    --->>??ACCESS TO THE FILE

    return $neuesBild->relObject('File.Filename');


    This produces an error:
    UNable to travers to related object field.

    Thanks in advance for any advice

    Salut
    Lukin

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: Accessing Fields of a has_one relation Link to this post

    Before we get to the question you asked you could have written the write code like this...

    $neuesBild = new UserBrille();
    $neuesBild->Name = $data->getVar('Name');
    $neuesBild->Email = $data->getVar('Email');
    $neuesBild->BildID = $data->getVar('BildID');
    $neuesBild->write();

    or even better like this...

    $neuesBild= new UserBrille($data);
    $neuesBild->write();

    and to get your has_one...

    $doFile = $neuesBild->Bild();

  • Lukin
    Avatar
    Community Member
    42 Posts

    Re: Accessing Fields of a has_one relation Link to this post

    hej swaiba thanks for the help

    $neuesBild= new UserBrille($data);
    is not working cause it wants an array and gets string (Name=test&Email=test@test.de&BildID=65)

    Does silerstripe offer a convert-function for that out of the box?

    salut

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: Accessing Fields of a has_one relation Link to this post

    ah yes, sorry older version... try...

    $neuesBild= new UserBrille($data->getVars());

  • Lukin
    Avatar
    Community Member
    42 Posts

    Re: Accessing Fields of a has_one relation Link to this post

    ah yeah thanks a lot man!

  • johnmblack
    Avatar
    Community Member
    61 Posts

    Re: Accessing Fields of a has_one relation Link to this post

    Hi @swaiba,

    > and to get your has_one...
    > $doFile = $neuesBild->Bild();

    This pattern doesn't ever seem to work for me. For example,

       public static $has_one = array(
          'PrimaryImage' => 'Image',
          'ThumbnailImage' => 'Image'
       );

       public function ThumbnailImage()
       {
          $imgObj = $this->ThumbnailImage(); // throws unknown PHP error here, not able to debug
          ...
       }

    I've also tried
       $imgObj = $this->getThumbnailImage(); // throws unknown php error
       $imgObj = $this->dbObject('ThumbnailImage'); // succeeds but doesn't return anything

    Am I missing something obvious? (very possible )

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: Accessing Fields of a has_one relation Link to this post

    throws unknown PHP error here, not able to debug

    hmmmm... not sure that makes sense to me... I can always debug, I use NetBeans to step through, Debug:: stuff and there are also some basic PHP / SS settings to ensure errors are recorded/shown. And I can't really help without some php error to work with!

  • johnmblack
    Avatar
    Community Member
    61 Posts

    Re: Accessing Fields of a has_one relation Link to this post

    oh DUH, it's because I used the same method name and caused an infinite recursion. I should have used parent:: .

    The following works now... templates can always call $ThumbnailImage and the data model provides the fallback, transparently:

       public function ThumbnailImage()
       {
          $imgObj = parent::ThumbnailImage();
          if (!$imgObj->exists()) $imgObj = $this->PrimaryImage();
          return $imgObj;
       }

    I also learned the hard way that a has_one object always "exists" in an object sense, so line 2 of the above function could not be "if (!$imgObj)" or "if (is_null($imgObj))" which works elsewhere. Here, we had to use the provided "exists()" method of the object.

    2278 Views
Page: 1 2
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.