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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

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

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

Accessing Fields of a has_one relation


Go to End


9 Posts   3893 Views

Avatar
Lukin

Community Member, 56 Posts

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

Avatar
swaiba

Forum Moderator, 1899 Posts

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

Avatar
Lukin

Community Member, 56 Posts

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

Avatar
swaiba

Forum Moderator, 1899 Posts

3 March 2011 at 1:10am

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

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

Avatar
Lukin

Community Member, 56 Posts

3 March 2011 at 1:13am

ah yeah :) thanks a lot man!

Avatar
johnmblack

Community Member, 62 Posts

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 anything

Am I missing something obvious? (very possible :-) )

Avatar
swaiba

Forum Moderator, 1899 Posts

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!

Avatar
johnmblack

Community Member, 62 Posts

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.

Go to Top