still return deleted entity in all contexts except one
I have a Doctrine entity ( FieldOption
) which represents one single option in a select field in a form (eg Red
, Green
, Blue
). A user, at any point in time, can opt to delete options from fields (eg removing Green
) and for this I have set the entity to be soft deletable using Gedmo, which works perfectly.
When an option that is stored against at least one submission of the form is deleted however, that submission entity (which has a relationship to the FieldOption
entity) fails to load as the FieldOption
is filtered out. Really, I only want the soft delete functionality to occur when returning the data for rendering the form - viewing existing records linked to that entity should still be able to load the soft deleted options by default.
Is there a way to disable this filter when retrieved via another entity, but ensure that the filter is enabled when loading the list of options directly?
The link commented by Sepultura gives a few examples as to how to enable/disable filters and write your own custom filters.
From the docs
Example filter:
<?php
namespace Example;
use DoctrineORMMappingClassMetaData,
DoctrineORMQueryFilterSQLFilter;
class MyLocaleFilter extends SQLFilter
{
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
{
// Check if the entity implements the LocalAware interface
if (!$targetEntity->getReflectionClass()->implementsInterface('LocaleAware')) {
return "";
}
return $targetTableAlias.'.locale = ' . $this->getParameter('locale'); // getParameter applies quoting automatically
}
}
The docs also say to use the following line of code to add this filter (so you can enable/disable it):
$config->addFilter("locale", "DoctrineTestsORMFunctionalMyLocaleFilter");
However, if you'd want to use this filter everywhere, you'd have to put this line of code in a spot you want everywhere, and also get access to $config
(which contains the Configuration
object from Doctrine).
In Zend Framework 2 and 3 you could add the following configuration to a core/mvc module or in the application configuration:
'doctrine' => [
'configuration' => [
'orm_default' => [
'filters' => [
'locale' => MyLocaleFilter::class,
],
],
],
],
The above line of code and configurations enable this filter, to disable in a specific location, use the following line where you want to disable (per the docs (the first enables & configures)):
<?php
$filter = $em->getFilters()->enable("locale");
$filter->setParameter('locale', 'en');
// Disable it
$filter = $em->getFilters()->disable("locale");
链接地址: http://www.djcxy.com/p/65042.html