I just ran into a weird bug with entity_load() returning the wrong result with the controller of the entity API. In my case the entity object creation triggered a rules cache rebuilt, which also needs to fetch some entities. Thus the controllers properties related to load() call were overwritten and the initial load() returned wrong results.
While I agree with catch that entity load calls triggering other load calls is something which one better avoids, it will still happen sometimes in practice as my case shows. Thus the suggested fix is to make the controller more robust, so recursed loads don't cause bugs. For that we may not set any load request related properties in the controller object, instead we just pass around the variables which was most times already the case anyway.
Attached is a patch that fixes the controller to do so. I've looked over all core controllers and replaced $this->ids with $ids and so on. While going through the code I also fixed a wrong comment I noted and I removed some dead code I found in the taxonomy controllers.
Comment | File | Size | Author |
---|---|---|---|
#6 | fix_comment.patch | 655 bytes | fago |
drupal_controller_fix.patch | 15.13 KB | fago | |
Comments
Comment #1
Frando CreditAttribution: Frando commentedYes, I thought about the same recently. We shouldn't set load-specific properties directly in the controller. Making the pass around explicit also makes it much clearer what e.g. buildQuery needs.
These two hunks in the taxonomy controllers - are you sure they're dead? I haven't actually checked the code and have no development environment at hand right now, so just wanted to make sure. Otherwise, RTBC from me after tests pass.
Comment #2
fagoOne hunk added a 'type' property to the taxonomy controller, which was nowhere used (there is no type at all). The other hunk from the vocabulary controller is related to a join on the node table, which isn't there any more.
Comment #3
catchPatch looks good to me, I found some dead code in #729968: Dead code in TaxonomyVocabularyController but looks like fago found more. This conflicts with #566940: Move node specific code out of entity.inc I think but that's an easier re-roll, let's get this in.
Comment #4
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks.
Comment #5
neclimdulfor what its worth, this breaks the function documentation.
Comment #6
fagoI'd not say that
parent::buildQuery()
means you have to call the function without arguments -parent::buildQuery()
just tells you what to call. That you have to pass the arguments when you call the parent method is normal, so I don't think we have to document that somehow.But yes I'd be good to have a @return, thus here is a patch that adds one.
Comment #7
catchComment #8
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks.