views:

77

answers:

1

Hi,

In my application, i have:

  • a table storing customers' orders, and
  • a table storing order entries

so basically a one-to-many relationship between the orders table and the orders entries table (should be quite a common structure).

i have a page that display all of a customer's orders (but not the entries). This part is simple, in my OrdersController, i have a viewAllOrdersAction which queries the DB, and send the results to the view for rendering.

Now, the next thing i want to do is, when the user clicks on a specific order, i want to go to a page which shows all the entries for that order.

i suppose i will need to add a viewOrderAction to the OrdersController which again queries the DB and send results to another view.

The question i have is regarding sending the orderId from the viewAllOrders page to the viewOrderAction and retrieving it there.

What is the right way to do this?

  1. Should i do a very simple form with a hidden orderId field and a link which does a submit? it does seem overkill...
  2. Or should i just compose a link URL with "?orderId=12345"? i would suppose this would be the better way, but how do i construct a URL with a GET param using the Zend_View's URL helper (i.e. the $this->url(...) in a view)?

Thanks!

+1  A: 

Option 2 is the best bet and it would look something like:

<?php echo $this->url(array('order_id' => $this->order->getId()), $viewOrderRouteName); ?>

Now if you didnt have a named route with module, contorller, action speced then you could:

<?php echo $this->url(array(
    'order_id' => $this->order->getId()
    'module' => 'customer',
    'controller' => 'order',
    'action' => 'viewOrder'
), 'default' ); ?>

Assuming you have the default route enabled. Note this just ouputs the url not a full a tag so use in it as the href attrib or use sprintf to compose the tag... I think there is a generic html tag helper so you could use that as well.

Now if you dont want to expose your order ids in the url you could come up with some sort of other params to select the order itself (although this would require a join query in your view order controller) like customer username && order date or what have you simialr to how you might see a blog permalink as a date && a title "slug".

prodigitalson
Thanks! :) Didn't know that the urloptions array can be used for request parameters as well. Surprising that the URL ended up as .../orderid/12345 instead of ...?orderid=12345 though it can still be obtained via $request->getParam('orderid').
Edwin Lee
Thats not odd thats the default route setup... unless you override it or supply it manually youre never going to see a `?*` query string - the url will always use slashes... You should really look at the docs for `Zend_Controller_Router` :-)
prodigitalson
i see. in itself that's not a problem. but say i have another form on that page which calls a different action, i.e. e.g. $form->setAction('another'), then it will actually append and use the url .../orderid/12345/another...
Edwin Lee
@Edwin: No there is a paramter to "reset" the params array so that the current params from the routed request arent used... i think its the third or forth.. look at the API for info.
prodigitalson