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

Parent-Child page and dataobject


Go to End


18 Posts   9368 Views

Avatar
Mackodlak

Community Member, 95 Posts

16 June 2011 at 8:38pm

Hello,
I am trying to make a front end editing for a custom class (LOTD extends DataObject) simmilar to the way a blog module did it.
I have made a patrent - child relationship between LOTD and LOTDPage (LOTDPage extends Page and displays LOTDs)
To edit LOTD I am using the funcion:

function EditURL() {
return $this->getParent()->Link('post') . '/' . $this->ID . '/';
}

which is in LOTD.Php and

function post(){
$page = $this->customise(array(
'Content' => false,
'Form' => $this->LOTDEntryForm()
));

return $page->renderWith('Page');
}

which is inside LOTDPage.php

when trying it out i get and error

[User Error] Uncaught Exception: Object->__call(): the method 'getparent' does not exist on 'LOTD'

so I guess I can't use getparent() for DataObject?
Or am I doing sth else wrong?
How to make that link?
Is there a better way to do this?

Avatar
Mackodlak

Community Member, 95 Posts

17 June 2011 at 2:20am

Mby I am using getparent wrong... dunno...
Any ideas how to make that link functional?

Avatar
martimiz

Forum Moderator, 1391 Posts

18 June 2011 at 10:55pm

I can't quite follow what you are doing, but that's probably because these are just bits of code :-) DataObjects don't really have Links, at least not by default. And I don't think a parent-child relationship works between Pages and DataObjects, but maybe you're referring to a has_one/has_many relation and defined a a custom getParent() method in your DataObject..?

Anyway, maybe this thread will be of some help:

http://silverstripe.org/general-questions/show/11323

Avatar
Mackodlak

Community Member, 95 Posts

20 June 2011 at 6:26pm

Edited: 20/06/2011 6:31pm

Yes, this is exactly what I need, but sadly, I got this part covered from reverse-engineering the blog module. I don't, however, have figured out how and where to call the edit form?
I have a form for entering a new LOTD at the beginning of LOTDPage, set up so that only admins can see it, but I have to be able to edit each of the LOTDs (DataObjects as you have observed) on the same page or somewhere else... The same page doesn't work (filling the existing form with LOTD data, as I have heared you might be able to do it using javascript which I can't), so I tried to create the page as it is done in blogmodule:
when you order a form for creating or editing of LOTD (create will make a new one, edit will fill it like in the link you showed me) you actually are calling a form that will be created on a new page wich isn't exactly a page...
Look at this 2 bits of code, how it's done in blog module:

/**
* Link for editing this blog entry
*/
function EditURL() {
return $this->getParent()->Link('post') . '/' . $this->ID . '/';
}

This one creates a LINK for the page (what I was doing and why I am using the getpParent - figured now can't be used on dataobjects, just pages)

/**
* Post a new blog entry
*/
function post(){
if(!Permission::check('BLOGMANAGEMENT')) return Security::permissionFailure();
$page = $this->customise(array(
'Content' => false,
'Form' => $this->BlogEntryForm()
));

return $page->renderWith('Page');
}

This one is "the" post from Link('post'), and it is not a Page for creating or editing, it is only rendered with Page. Ofc if I understand it correctly, and BlogEntryForm is form that checks if there is a form or does it have to create a new one, creates new Fields and either fills it or leaves them blank in case of new entry...
Basically I wanna do the same, only not with Page but with DataObject, and not with BlogEntryForm but MyLOTDForm (which is similar to BlogEntryForm, but with my own fields)

If you have any idea how to make it differently (I am happy with whatever you can give me, as long as edit works...) or how to fix the link so it works, I will be very happy!

Thank you in advance!

Avatar
Mackodlak

Community Member, 95 Posts

28 June 2011 at 8:34pm

Hello, this is not exactly a bump.
I forgot about this for some time, been doing some other stuff...
Now it got to me that I have to finish this edit thingy, so I'm back on it again.
What i did so far:
nothing much... thing still doesn't work

it's a work in progress. I will upload all of the important files so you can help me, yes?

The problem:

[Warning] reset() expects parameter 1 to be array, null given
GET /ss/svijet-sigurnosti/lotd/post/45/

Line 102 in /var/www/ss/sapphire/core/ClassInfo.php

Source

93 * Return the root data class for that class.
94 * This root table has a lot of special use in the DataObject system.
95 *
96 * @param mixed $class string of the classname or instance of the class
97 * @return array
98 */
99 static function baseDataClass($class) {
100 global $_ALL_CLASSES;
101 if (is_object($class)) $class = get_class($class);
102 reset($_ALL_CLASSES['parents'][$class]); <============ this here is problem
103 while($val = next($_ALL_CLASSES['parents'][$class])) {
104 if($val == 'DataObject') break;
105 }
106 $baseDataClass = next($_ALL_CLASSES['parents'][$class]);
107 return $baseDataClass ? $baseDataClass : $class;
108 }

The idea:

as I said, it's a work in progress, some things are not done yet and so the template might not make sense, but I have been changing some stuff so I can edit it.
Izmjeni in template is supposed to be the link for edit, Brisanje is link for delete. Delete works, edit does not.

GetLOTDs is looping through all of LOTDs and in template I am displaying them all in <% control GetLOTDs %>
While displaying them I am creating links for delete and edit, as I have said be4, delete works, permissions work, edit does not.
The idea - pressing Izmijeni (edit) calls EditURL that is gonna form a link with calling function post in LOTDPage and givind the LOTD $ID to it. Post basically calls function for editing LOTD and places it on "page" with $page->renderWith('Page');

As far as I can see, this "should" work, or sth simmilar, cause edit in blog module works like this...
I must have made some mistake, don't know where.
Could som1 debug/fix this for me?

While you're at it, don't know how to make tags work, but this is more important.

Thank you!

Attached Files
Avatar
martimiz

Forum Moderator, 1391 Posts

28 June 2011 at 9:47pm

I did a quick browse through your code, didn't go as far as testing it :-) two things I noticed:

in LOTDEntryForm():

1. Did you check $this->request ever returning an ID? I don't think that works any more - you might try $this->requestParams()

2. I sort of feel that if(!$ID == 0) might return true even if $ID == 0. Maybe replace it by something like if ($ID) or safer: if (intval($ID) > 0) or...

what might happen is: you never get any ID, still DataObject::get_by_id() is always hit, and since you don't check if it actualy returns anything, it tries to load the form from an empty array - giving you the error you mention. OK, I didn't test it, but just maybe this might be it?

Avatar
Mackodlak

Community Member, 95 Posts

28 June 2011 at 11:34pm

OK, I have tried both suggestions:

if (intval($ID) > 0) doesn't change anything

$this->requestParams()->latestParam('ID'); returns following error:

[User Error] Uncaught Exception: Object->__call(): the method 'requestparams' does not exist on 'LOTDPage_Controller'
GET /ss/svijet-sigurnosti/lotd/post/45/

Line 724 in /var/www/ss/sapphire/core/Object.php
Source

715
716 default :
717 throw new Exception (
718 "Object->__call(): extra method $method is invalid on $this->class:" . var_export($config, true)
719 );
720 }
721 } else {
722 // Please do not change the exception code number below.
723
724 throw new Exception("Object->__call(): the method '$method' does not exist on '$this->class'", 2175);
725 }
726 }
727
728 // -----------------------------------------------------------------------------------------------------------------
729
730 /**

so I don't think that'll work
Thanks though, I will try more stuff.

Avatar
martimiz

Forum Moderator, 1391 Posts

29 June 2011 at 1:24am

$this->requestParams('ID')

Go to Top