Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SoftDeleteable] Deleted entities showing up on eager fetches #1463

Closed
sonu27 opened this issue Nov 25, 2015 · 11 comments
Closed

[SoftDeleteable] Deleted entities showing up on eager fetches #1463

sonu27 opened this issue Nov 25, 2015 · 11 comments

Comments

@sonu27
Copy link

sonu27 commented Nov 25, 2015

Using v2.4.7 and Symfony 2.7.7 (also tested on Symfony 2.7.6), if I use fetch="EAGER" on one-to-many relations the "deleted" entities appear in the results.

E.g.
<one-to-many field="items" target-entity="BasketItem" mapped-by="basket" fetch="EAGER"> <cascade> <cascade-all/> </cascade> </one-to-many>

Is this desired behaviour? Could I be doing something wrong?

orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        default_entity_manager: default
        entity_managers:
            default:
                connection:    default
                mappings:
                    AppBundle: ~
                result_cache_driver:
                    type: redis
                    host: "%redis_host%"
                    instance_class: Redis
                filters:
                    softdeleteable:
                        class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                        enabled: true
@l3pp4rd
Copy link
Contributor

l3pp4rd commented Nov 26, 2015

it might be that eager fetching does not hit filter, so far I do not know how you could by pass it.. but in the end it is better to handle softdeletion manually. managing softdeleted relations is though task.

@sonu27
Copy link
Author

sonu27 commented Nov 27, 2015

Found someone else with the same problem it seems: http://sofd.developer-works.com/article/21370516/Doctrine+2+filtering+with+EAGER+relations

@datasage
Copy link

How EAGER mapping is handled has changed with doctrine 2.5. It now uses a join instead of two separate queries. The filter does not get added to the join.

@l3pp4rd
Copy link
Contributor

l3pp4rd commented Nov 30, 2015

I see, if that is possible, maybe there is a way to hook it in sql filter, if that issue is a concern, you could have a look

@simon-rad
Copy link

Tried to find a workaround that problem.
Added posibility to suply eager relations like that:

/**
 * @ORM\Table()
 * @ORM\Entity
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, eager={"template_view"})
 */

in filter listener

public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)

added a code to handle configuration param

if(isset($config['eager']) && $config['eager'])
{
    $mainCondition = "({$addCondSql})";
    $softColumn = $targetEntity->getQuotedColumnName($config['fieldName'], $platform);
    $platform = $conn->getDatabasePlatform();

    foreach($config['eager'] as $eagerRelation)
    {
        $softCondition = $platform->getIsNullExpression($eagerRelation.'.'.$softColumn);
        $addCondSql = "{$mainCondition} AND ({$softCondition})";
    }
}

That would generate query:

SELECT ... FROM template t0 LEFT JOIN template_view t5 ON t5.template_id = t0.id 
WHERE (((t0.deleted_at IS NULL) 
AND (template_view.deleted_at IS NULL)))':

The problem is that there is no way to find out what is the alias for template_view table

@l3pp4rd
Copy link
Contributor

l3pp4rd commented Mar 16, 2016

I see @Wobbly-Wibbly in general softdeleteable does not fit its concept well with orm, most of the users fallback to managing it manually, same for myself in such cases sadly. Keeping it open since there is no obvious solution so far

@sonu27
Copy link
Author

sonu27 commented Mar 16, 2016

Nowadays, Soft deletes are an anti-pattern but many legacy projects use it.

However, it might be worth mentioning this compatibility problem on the SoftDeleteable docs.

@datasage
Copy link

While there are better ways to solve the problems (logging, auditing, archiving) that soft delete is intended to solve. It is an easy option to use when time or resources are not available to implement a better option. Its also very easily understood.

Of course it can cause other problems later, depending on the application, so its a tradeoff.

@Padam87
Copy link
Collaborator

Padam87 commented Mar 16, 2016

Softdelete is definitely not an anti-pattern. Some computer science people may think so, but real word use cases, and RAD requires the softdeleteable pattern.

I wrote a failing test for this issue: #1550

Unfortunately, It should be fixed by doctrine.

@l3pp4rd
Copy link
Contributor

l3pp4rd commented Mar 16, 2016

thanks for the input, will need to mention this in documentation.

@seyfer
Copy link

seyfer commented Oct 19, 2017

Did somebody create an issue in Doctrine about that problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants