Reputation: 11855
I have a status_id
field in everything which manages if things are published etc. I've created this in my AppModel to take care of all the filtering.
public function beforeFind($queryData) {
// Force all finds to only find stuff which is live
$queryData['conditions'][$this->alias.'.status_id'] = 1;
return $queryData;
}
Which works fine, until you go into the admin routing at which point you want to see all the items, and they are being filtered. Is there a way to know if you are in admin routing from the AppModel?
Upvotes: 2
Views: 5680
Reputation: 368
I have experience in a similar situation whereby I use a "soft delete" behavior.
My recommendation is to check whether or not you already have a condition on the status_id
field and only set it if you don't. Like so:
public function beforeFind($queryData) {
if (! isset($queryData['conditions'][$this->alias.'.status_id'])) {
// Force all finds to only find stuff which is live
$queryData['conditions'][$this->alias.'.status_id'] = 1;
} elseif ($queryData['conditions'][$this->alias.'.status_id'] == 'all') {
unset($queryData['conditions'][$this->alias.'.status_id']);
}
return $queryData;
}
This way you have the desired functionality by default and you can also use normal conditions on the status_id
field when needed.
As a bonus, to ignore the condition, like you want in admin routing, you can use this:
$data = $this->paginate('YourModel', array('YourModel.status_id' => 'all'));
It's basically just a convenience to avoid having to pass all the status IDs as a "fake-ish" condition.
That's how I would do it. Hope it helps.
Upvotes: 4
Reputation: 11855
The answer I've got would be to set the route as an attribute in the model from the controller, which you can then check from the Model.
However CakeDC have a Utils plugin bundle which includes a SoftDeletable behaviour which I use now. https://github.com/cakedc/utils
Upvotes: 0
Reputation: 2432
there is no reason for a model to know what the route is. It's purpose is the feed the controller with what's requested. In another word, you must set the condition in the controller, not in the model.
Sorry for having been too brief.
What you're trying to do goes against the MVC design pattern. It's the controller that handles and routes requests made by the client.
In your controller:
function index(){
$data = $this->paginate('YourModel', array('YourModel.status_id' => 1));
$this->set('data', $data);
}
function admin_index(){
$data = $this->paginate();
$this->set('data', $data);
}
Even less verbose
You are trying to detect in the model if the request has been made from the admin routing, which is the wrong way to go. The controller handles that automatically. admin_index() handles index request made through the admin route, index() request made through normal route.
It is in the controller that you decide what data you want. You should move the logic you have in your beforeFind method into your controller, like in the example above.
Upvotes: 0
Reputation: 513
You can check for admin route in your controller and define it like this:
App Controller:
if(isAdmin){
$queryData['conditions']['isAdmin'] = true;
}
In your AppModel:
if(!isset($queryData['conditions']['isAdmin'] & $queryData['conditions']['isAdmin'] !== true )){
$queryData['conditions'][$this->alias.'.status_id'] = 1;
return $queryData;
} else {
return $queryData
}
This is a dirty way but should work.
Upvotes: 1