passing parent id when adding child items?

122 views
Skip to first unread message

Craig

unread,
May 8, 2012, 6:07:40 PM5/8/12
to joomla-de...@googlegroups.com
I've had some trouble getting this to work - basically, I have a list of items, and I click a link on that list to get to a list of child items. From there, I want to be able to add a child item, and I need to pass the parent id along so the new child can be associated with its parent.

The other part I need is for the parent id to be available to the child list after a successful add/edit is saved. 

I got the first part working with a hack - in my child controller's add function, I'm sticking the parent id from the request into user state under a custom context, so it survives the add to edit redirect, and then I'm pulling it out in my model's loadFormData. This is ugly - I wouldn't recommend it, and I expect it to break.

For the second part, if the add/edit save works, in my child controller's getRedirectToListAppend I'm getting the parent id from the form data on the request ($data = JRequest::getVar('jform', array(), 'post', 'array');), which also seems fragile...

So - is there a standard way of doing this that would just work?

Herman Peeren

unread,
May 9, 2012, 1:52:51 AM5/9/12
to joomla-de...@googlegroups.com
I normally use hidden variables for that in all forms (including the lists).
<input type ="hidden" name = "parent_id" value="thevalue">
It would probably be more Joomla-style since 1.6 to use JForm also for the hidden fields, but I haven't used it for this until now. See http://ostraining.com/howtojoomla/how-tos/development/getting-started-with-jform

On each level I get this MVC-cycle:
  • controller reads the parent-id from the request
  • controller puts the parent-id in the model's state
  • view reads the parent-id from the model and puts it in the view/layout (hidden variable)

Now it looks like you have an extra redirect after saving. Then you'll probably have to put the variables in the request-object: JRequest::setVar().

Instead of putting it in a hidden variable it is also possible to put variables directly in the form's action-attribute. But I prefer to have all passed variables nicely together in a list of hidden variables.

Would this help you? Or am I misunderstanding you? 

Regards,
Herman

Craig

unread,
May 9, 2012, 8:54:07 AM5/9/12
to joomla-de...@googlegroups.com
Thank you for your answer - it was very helpful.

I was using user state variables because I didn't realize the model state would survive across requests and that it was session-specific... model state looks like what I need. 

The extra redirect is actually what JControllerForm does after a successful save. I was overriding getRedirectToListAppend to add the parent id here, but I can't count on this always having a form variable - but I should be able to get the parent id from the model state here as well. 


Craig

unread,
May 9, 2012, 9:39:02 AM5/9/12
to joomla-de...@googlegroups.com
This didn't seem to work - in my form controller add method, I set the parent id on the state, then in my form model loadFormData method I try to get it; 

I verified I am setting it correctly with:
controller add()
$model = $this->getModel();
$model->setState('group_id', JRequest::getVar('group_id'));

but the read isn't finding the data
model loadFormData()
$this->getState('group_id')

Am I doing something wrong, or does model state not persist between requests? 

Craig

unread,
May 9, 2012, 10:37:17 AM5/9/12
to joomla-de...@googlegroups.com
The model state does not seem to be available across requests, so I switched back to using user state. 

Herman Peeren

unread,
May 9, 2012, 11:02:33 AM5/9/12
to joomla-de...@googlegroups.com
Yep, model state is not available across requests. That's why I would pass any variables to the next request if you have to redirect. That seems like a better solution to me. But I'll have a look at the code first before answering "in the blue".

Craig

unread,
May 9, 2012, 2:51:43 PM5/9/12
to joomla-de...@googlegroups.com
Ah - I can probably override getRedirectToItemAppend to pass along the parent id - the JControllerForm add function has:

// Redirect to the edit screen.
$this->setRedirect(
JRoute::_(
'index.php?option=' . $this->option . '&view=' . $this->view_item
. $this->getRedirectToItemAppend(), false
)
);


Reply all
Reply to author
Forward
0 new messages