Restrict objects available in symfony admin generator for all actions
One can change the query that is used for the list action in an admin generator configuration by using the table_method
option. For example,
# apps/backend/modules/job/config/generator.yml
config:
list:
table_method: retrieveBackendJobList
But I want to restrict all the actions in the admin generator. In particular, I want to restrict all the objects displayed, edited, deleted, etc., to objects that have a particular attribute that may depend on the c开发者_如何转开发urrent day of the week or the time of the day.
I don't want to override the model class because for other apps I want the restriction to be different (or perhaps no restriction at all).
Where (i.e., which file(s)) and how can I make this change?
Probably via the table_method and the generator.yml features to add and remove action buttons you've already taken care of customizing the index (i.e. list) action.
For the other actions, you might wish to create additional methods in the models for these custom queries. But the place to override the default behavior is in the actions.class.php file for the module.
So in your example, you'd edit the apps/backend/modules/job/actions/actions.class.php file and write custom code for each action you need to alter.
So for example you could change the delete behavior like this:
# apps/backend/modules/job/actions/actions.class.php
require_once dirname(__FILE__).'/../lib/jobGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/jobGeneratorHelper.class.php';
class jobActions extends autoJobActions
{
/**
* Override standard delete action.
* @param sfWebRequest $request A request object
*/
public function executeDelete(sfWebRequest $request) {
if ($some_custom_condition) {
$job = Doctrine_Core::getTable('job')->find($request->getParameter('id'));
$job->delete();
$this->getUser()->setFlash('notice', 'Record deleted.');
return sfView::SUCCESS;
} else {
$this->getUser()->setFlash('error', 'You do not have permission to do that.');
return sfView::ERROR;
}
}
}
Use routing for that. This will ensure that all actions will have one method for object query.
job:
class: sfDoctrineRouteCollection
options:
model: Job
module: job
with_wildcard_routes: true
model_methods:
object: getActiveJob
class JobTable extends Doctrine_Table
{
public function getActiveObject($params)
{
$q = $this->createQuery('j')
->where('j.id = ?', $params['id'])
->addWhere('j.is_active = ?', true)
;
return $q->fetchOne();
}
}
In your custom actions use $this->getRouting()->getObject() to make use of this method.
精彩评论