2

I'm trying to filter data in my view based on URL parameters. I'm currently using basic Yii Ajax search in my view, which uses $_POST to get the results to display in CGridView.

search() method in my model is also a default one, using CDbCriteria and compare() to return CActiveDataProvider based on criteria submitted via mentioned search in view.

The grand question here is:

How can I perform search via URL which would look like http://example.com/index.php/something/listAll?id=10&owner=numline1?

Thanks!

Numline1
  • 159
  • 7
  • 1
    Are you wanting the AJAX POST to work using the parameters passed in through the GET url string? – Michael Bellamy May 22 '15 at 14:21
  • Not really, I need the search to work with URL/$_GET parameters, the $_POST is just a method I provided to explain how it works now. $_GET method should work without any ajax and just provide filtered data to a current view (in this case listAll). – Numline1 May 22 '15 at 14:32
  • I'm not sure how you're going to filter data using the GET parameters without submitting those to your AJAX call so it know's what data to get. Three's a strong chance I'm not understanding you though so apologies for that. – Michael Bellamy May 22 '15 at 14:38
  • No problem, you may be actually correct. I'm not 100% sure what's the dataflow when submitting search form, but the point is that neither controller or anything within Ajax in my code checks for $_GET parameters at all. I believe that CGridView only gets already filtered records from controller, so I'd focus on that. – Numline1 May 22 '15 at 14:55
  • I'm not 100% on your actual process. But for me, I'd have my POST data simply getting the data that I require - if the GET parameters are set, I'd be passing those as a filter to the POST call. If that makes sense :) – Michael Bellamy May 22 '15 at 15:42

1 Answers1

1

Solved it after some debugging. So it turns out, I need something like this in my controller:

if(isset($_GET['Book']))
    $model->attributes=$_GET['Book'];
else if(isset($_POST['Book']))
    $model->attributes=$_POST['Book'];

And the actual search URL will look like this:

http://example.com/index.php/books/listAll?Book[id]=1234&Book[name]=Lorem%20Ipsum
Numline1
  • 159
  • 7