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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

only able to access ID of dataobject, not able to access other parts


Go to End


11 Posts   2570 Views

Avatar
vancouverWill

Community Member, 121 Posts

3 June 2011 at 12:46pm

I have had this problem plenty of times over the last couple of years and haven't been able to understand why. For example I create a dataobject called Technology and add a few variables into the database. When I try to access any of them from other functions or from templates in the database by calling $this->URLSegment or $this->TechnologyPublisher I just get nothing. No error it seems that it can't see them at all.

lass Technology extends DataObject
{
static $db = array (
'Type' => 'Text'
, 'URLSegment' => 'Text'
, 'TechnologyPublisher' => 'Text'
, 'TechnologyCertification' => 'Text'
, 'TechnologyDescription' => 'Text'

so I tried other methods such as the below

Debug::show($this->ID);
Debug::show($this->dataRecord->TechnologyPublisher);
Debug::show($this->getField("TechnologyPublisher"));
Debug::show($this->dboject("TechnologyPublisher"));

but always nothing. I have always had to resort to calling the object from the db by

$area = DataObject::get_by_id('Technology',$id);

then I can do $area->TechnologyPublisher

but this is really frustrating as it seems like I am creating unnecessary work for myself and SS but I really cant see any problem I have created. any ideas?

thanks

Will

Avatar
Willr

Forum Moderator, 5523 Posts

6 June 2011 at 3:43pm

Could you provide an example of where you are calling $this?

Debug::show($this->dataRecord->TechnologyPublisher);

FYI dataRecord is only used in controllers to point to dataobjects not just dataobjects.

Avatar
vancouverWill

Community Member, 121 Posts

7 June 2011 at 12:07pm

Thanks for the help Willr. I was trying $this->TechnologyPublisher first but that wasn't working so start trying other solutions to access the class's db information. The situation I come up with this the most is when I try to link to a dataobject. Heres the code

<?php
class Technology extends DataObject
{
static $db = array (
'Type' => 'Text'
, 'URLSegment' => 'Text'
, 'TechnologyPublisher' => 'Text'
, 'TechnologyCertification' => 'Text'
, 'TechnologyDescription' => 'Text'
);

public function Link()
{
Debug::show($this->TechnologyPublisher);
Debug::show($this->dataRecord);
Debug::show($this->ID);
Debug::show($this->dataRecord->TechnologyPublisher);
Debug::show($this->getField("TechnologyPublisher"));
Debug::show($this->TechnologyPublisher);
Debug::show($this->dbObject("Title"));
Debug::show($this->dbObject("TechnologyPublisher"));
//code below here is actally used
$id=$this->ID;
$area = DataObject::get_by_id('Technology',$id);
$url = $area->URLSegment;
if($Page = DataObject::get_one("ProjectPage"))
return $Page->Link() ."display/".$this."/".$url;
}

I know I'm debugging a lot here but that was all the different ways I tried to get basic class information. If I print_r($this); I can see that it is handled the correct class but it only shows the information of a standard dataobject, title, content, date created etc. So I'm confused cause I can take the class but it seems like I am only gettting limited access to it(even though the function is called in the same class)

Avatar
Willr

Forum Moderator, 5523 Posts

7 June 2011 at 2:02pm

You sure you're not calling this class statically using singleton() or Technology::Link() rather than calling it on an instance.

Avatar
vancouverWill

Community Member, 121 Posts

8 June 2011 at 7:08am

i'm not 100% sure but when I run $this->ID i;m getting the correct object ID, wouldn't that show it's not static? How would I end up calling the singleton() function by accident.

Thanks for your help

Avatar
martimiz

Forum Moderator, 1391 Posts

8 June 2011 at 8:40am

Do you have example code showing from where you call the Link() method? And how you get your (set of) Technology DataObject(s) in the first place? Could it be you're operating on a generic DataObject by mistake?

Avatar
vancouverWill

Community Member, 121 Posts

15 June 2011 at 7:40am

thanks for the help, sorry for the slow reply by me but had other projects which to take precident at work this week. I have been trying to replicate this problem with different dataobjects to understand the problem but from your last answer you may be right, the way I get to the objects in the first place could be the problem.

I am using the custom search files shown in the (CustomSearchForm snippet) http://silverstripe.org/all-other-modules/show/6641?start=24 forum post.

On Page_results.ss it loops through the successful search objects with ( <% control Results %>) and $Link calls the function pointing to the dataobject. Because of some custom URLs I've used the standard link function wasn't suitable. In a DO called Techonology.php I have

class Technology extends DataObject
{
	static $db = array (
		'Type' => 'Text'
		, 'URLSegment'	=> 'Text'
		, 'TechnologyPublisher' => 'Text'
		, 'TechnologyCertification' => 'Text'
		, 'TechnologyDescription' => 'Text'
	);

......

 public function Link() 
   { 
   	$id=$this->ID;
	$area = DataObject::get_by_id('Technology',$id);
	$url = $area->URLSegment;                                                         // notice here I would have prefered to just call $this->Technology
   	if($Page = DataObject::get_one("ProjectPage"))
      return $Page->Link() ."display/".$this."/".$url;
   } 

see in the Link() function I have had to call the dataobject by ID even though $this already shows the ID. I can't see why if $this is a Project object I can't access the other fields?

Avatar
martimiz

Forum Moderator, 1391 Posts

15 June 2011 at 8:39am

I did take a really quick look at the searchfiles you mentioned, so I might have this all wrong, but I think in this case the resultset is created from a custom sql query where only a limited number of fields (amongst which ID and ClassName) are retrieved from the database for each class to be searched and to which some extra fields are then added and set to null. I guess to provide a generic way to create a search method that is valid for combinations of different classes.

Btw one of the fields set to null seems to be the URLSegment - see the searchEngine() method in the CustomSearchForm class

So in fact what you get in result is a set of generic objects based on all classes that are searched, with a general (sub)set of fields and where URLSegment is always null (?).

So you still need to retrieve the 'real' objects from database in one way or another using these data... If I'm wrong, anyone, let me know...

Go to Top