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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Preview: DataObjectManager module


Go to End


379 Posts   95930 Views

Avatar
drye

Community Member, 49 Posts

14 March 2009 at 8:25pm

Edited: 14/03/2009 8:27pm

ok, well I got it to work, learned that you can't use $this->MyBoolean inside a getMyBoolean, had to change function to getMyBooleanField.

Now that seems to work for viewing, but the edits don't persist. Any Ideas?

http://pastie.org/415982

Avatar
UncleCheese

Forum Moderator, 4102 Posts

15 March 2009 at 5:04am

You're not understanding how a custom getter works.


$manager = new DataObjectManager(
$this,
'MyDataObjects',
'MyDataObject',
array('Title' => 'Title, 'Published' => 'Published')
);

class MyDataObject extends DataObject
{
static $db = array (
'Title' => 'Varchar(100)',
'Published' => 'Boolean'
);

public function getPublished()
{
return $this->Published ? "Published" : "Unpublished";
}
}

That's all you need. When drawing the values for each record, SS first looks for a function named get[PropertyName]. If that function exists, it returns the value of that function. Otherwise, it just returns the value of PropertyName, which in the case of a boolean, is a 1 or 0.

Avatar
drye

Community Member, 49 Posts

15 March 2009 at 5:29am

I appreciate your comments, however I do understand how the get functions work. Your code will not work, you get an error. $this->Published is undefined, by asking in the IRC channel I found out that it was because that basically creates infinite recursion. You are calling the get function you just defined within it's self by asking for $this->Published. So I had to write a differently named getter to get the field I wanted, I want an ACTUAL check box field so I need to return a field. Well my code achieves this portion of my question. The open question I have is it possible to directly edit the data from the table and not within the light box? I have a checkbox, and I can change it's state from the table, but the changes do not persist. On a similar note, what if you wanted the text to be editable from the table, is this possible?

Again, I appreciate your help! Thanks again!

Avatar
UncleCheese

Forum Moderator, 4102 Posts

15 March 2009 at 5:47am

$this->Published is undefined because in your class you called it "Publish", not "Published".

Do this..

$manager = new DataObjectManager( 
$this, 
'MyDataObjects', 
'MyDataObject', 
array('Title' => 'Title, 'Published' => 'PublishedValue') 
);

class MyDataObject extends DataObject
{
static $db = array (
'Title' => 'Varchar(100)',
'Published' => 'Boolean'
);

public function getPublishedValue()
{
return $this->Published ? "Published" : "Unpublished";
}
}

If you want to edit records directly on the table, use a TableField. It will limit you to simple editing, however, so if you have checkbox sets or file uploads, or textareas, it isn't an effective solution. DataObjectManager is an upgrade to ComplexTableFields, not regular TableFields.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

15 March 2009 at 5:54am

Scratch that.. that code I sent you was wrong. I put PublishedValue in the headers array. I'm not thinking straight today.

For your example all you really need is a getPublish() function on your DataObject. And if this doesn't work:

return $this->Publish ? "Published" : "Unpublished";

Then try:

return $this->Publish == 1 ? "Published" : "Unpublished";

Worst case:

return $this->getField('Publish') == 1 ? "Published" : "Unpublished";

The first one should work fine, though. It's what I've always used. I think it failed last time because you were using the wrong property name.

Avatar
drye

Community Member, 49 Posts

15 March 2009 at 5:58am

No, I was just sticking with your example. I am not a complete noob :). For me $this-Publish is also undefined inside of a function called getPublish() (the whole recursive thing). And as for the editing, I am aware of table fields, but I was trying to achieve editing inside a dataObjectManager. If it can't be done, that is fine, just wanted to ask to make sure before I move on.

Thanks again!

Avatar
Bo_Einzeller

Community Member, 18 Posts

17 March 2009 at 11:39pm

Hy UncleCheese
I found some bugs:
1) If you use $manager-> setDefaultView($type) you can’t switch the view anymore.
The value of the constructor (with $_REQUEST['ctf'][$this->Name()] is overwriten.

2) I still have problems with 2 FileDataObjectManager. If the first one is in grid-view, the second can’t be ordered in list view, only in grid view. If the fist one is in list view, everything is ok. I think the problem is caused by the id “list-holder”. This id is not singular.

3) If i have files in a FileDataObjectManager, icons aren’t displayed in grid-view on first run. If I switch the view 2 times, the icons are displayed.

4) I’ve added some translation tags. See the .diff-File below. Its for the Revision 80 of your svn.
Cheers

Avatar
UncleCheese

Forum Moderator, 4102 Posts

18 March 2009 at 5:00am

Bo, you're the man! Thanks for these great catches.

1) setDefaultView() -- FIXED. Stored default_view as separate property.

2) Sorting issues on multiple FileDataObjectManagers. -- FIXED. You were correct about the duplicate ID.

3) File icons not displaying. Very weird. The code I was using shouldn't have worked, but for some reason it was working on one and failing on the other. Icon is now obtained correctly through $this->item->fileField->Icon() instead of $this->fileField->Icon() in FileDataObjectManager_Item.

4) Thank you for your work on the translations. I've checked them in. (Hope they were tested!)

Go to Top