21302 Posts in 5736 Topics by 2603 members
| Go to End | Next > | |
| Author | Topic: | 1479 Views |
-
Accessing Fields of a has_one relation

2 March 2011 at 11:58pm
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 -
Re: Accessing Fields of a has_one relation

3 March 2011 at 12:40am
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();
-
Re: Accessing Fields of a has_one relation

3 March 2011 at 1:06am
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
-
Re: Accessing Fields of a has_one relation

3 March 2011 at 1:10am
ah yes, sorry older version... try...
$neuesBild= new UserBrille($data->getVars());
-
Re: Accessing Fields of a has_one relation

14 May 2011 at 3:07am
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 anythingAm I missing something obvious? (very possible
) -
Re: Accessing Fields of a has_one relation

14 May 2011 at 3:23am
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!
-
Re: Accessing Fields of a has_one relation

14 May 2011 at 3:36am
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.
| 1479 Views | ||
| Go to Top | Next > |



