views:

44

answers:

1

A submitted form on my site returns an array of request data that is accessible with

$data = $this->getRequest();

This happens in the controller which gathers the data and then passes this array to my model for placing/updating into the database.

Here is the problem. What if I want to manipulate one of the values in that array? Previously I would extract each value, assigning them to a new array, so I could work on the one I needed. Like so:

$data = $this->getRequest();
$foo['site_id'] = $data->getParam('site_id');
$foo['story'] = htmlentities($data->getParam('story'));

and then I would pass the $foo array to the model for placing/updating into the database.

All I am doing is manipulating that one value (the 'story' param) so it seems like a waste to extract each one and reassign it just so I can do this. Additionally it is less flexible as I have to explicitly access each value by name. It's nicer to just pass the whole request to the model and then go through getting rid of anything not needed for the database.

How would you do this?

+2  A: 

Edit again: Looking some more at your question what I am talking about here all goes on in the controller. Where your form`s action will land.

Well you have a couple of options.

First of all $_GET is still there in ZF so you could just access it.

Second there is:

$myArray = $this->_request->getParams();

or

$myArray = $this->getRequest()->getParams();

Wich would return all the params in an array instead of one by one.

Thirdly if the form is posted you have:

$myArray = $this->_request()->getPost();

Wich works with $this->_request->isPost() wich returns true if some form was posted.

About accessing all that in your view you could always just in controller:

$this->view->myArray = $this->_request->getParams();

edit: right I taught you meant the view not the model. I guess I do not understand that part of the question.

If you want to deal with the post data inside your model just:

$MyModel = new Model_Mymodels();

$data = $this->_request->getParams();
$data['story'] = htmlentities($data['story']);
$myModels->SetItAll($data);

And then inside your model you create the SetItAll() function (with a better name) and deal with it there.

Edit: oh wait! I get it. You hate sanytising your input one by one with your technique. Well then what I showed you about how to access that data should simplify your life a lot.

edit: There is always the Zend_Form route if the parameters are really coming from a form. You could create code to interface it with your model and abstract all this from the controller. But at the end of the day if you need to do something special to one of your inputs then you have to code it somewhere.

Iznogood
+1 -- if you absolutely need the request in the model I guess you could do this. OTOH only the action controller should really be playing with the request.
Billy ONeal
@Billy ONeal yeah well I was really talking about controller actions. Took me a while to understand he was probably accessing all that directly in his model. But I posted an example of passing that data to his model. Thanks for the vote!!
Iznogood
This is all happening in the controller. I just pass the final result back to the model so it can update the database.
rg88
@rg88 well does my answer help? Otherwise please edit your question so we can uderstand what it is you want :)
Iznogood
I did but it seems it is still unclear? I want to run one of the various posted elements through htmlentities. So out of the many posted elements, how to I run just one of them through htmlentities before I go ahead and pass the data to the model for placing into the database?
rg88
@rg88 well after transfering yoru array with ->getParams() you can modify one and still pass that array to the model. I'll edit the end of my answer to show this. Or as I said if its that bad just use $_GET straight.
Iznogood
I think the difference here is that with my solution you do not have to call getparam() many times. just once to get your post data then its a normal array.
Iznogood
Added a little thing about Zend_Form at the end.
Iznogood
One thing about using Zend Form... this site uses jquery grid to generate the form and I'm not sure how to integrate that with Zend Form.
rg88
@rg88 well thats for another question my friend. But basically look into viewhelpers with Zend_Form. Allows 100% custumisation of the form itself and keeps the power of the class for the backend.
Iznogood
Thanks for the input Iznogood. Yuzgood.
rg88
@rg88 No problems!!
Iznogood