Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Removed early bail for empty arrays #4961

Closed
wants to merge 1 commit into from
Closed

Removed early bail for empty arrays #4961

wants to merge 1 commit into from

Conversation

TonyBogdanov
Copy link

It seems to me, that with this segment I've removed, it is not possible to clear a collection if the passed $data is an empty array.

If I am wrong, then what is the correct way to empty a collection? I've had this issue for a long time and this actually fixed it for me. At least when using in conjunction with Doctrine 2 entities.

Haven't tested it!

It seems to me, that with this segment I've removed, it is not possible to clear a collection if the passed $data is an empty array.

If I am wrong, then what is the correct way to empty a collection? I've had this issue for a long time and this actually fixed it for me. At least when using with conjunction with Doctrine 2 entities.

Haven't tested it!
@TonyBogdanov
Copy link
Author

Just found a case where this fix breaks other functionality in a really weird manner. I suppose this commit becomes unnecessary, but the main question remains: how does one remove all elements of a collection?

@weierophinney
Copy link
Member

@bakura10 , @macnibblet , any ideas to throw the way of @TonyBogdanov ?

@macnibblet
Copy link
Contributor

@TonyBogdanov What hydrator are you using ? I can't really provide any feedback until tomorrow morning. And can you please share your collection configuration

@TonyBogdanov
Copy link
Author

I am creating the collection using annotations on a Doctrine 2.3 entity, this is the markup:

/**
 * @ORM\OneToMany(targetEntity="Shop\Entity\ProductPromotion", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
 * @Annotation\Type("Zend\Form\Element\Collection")
 * @Annotation\Options({
 *     "label" : "_Promotions",
 *     "count": 0,
 *     "allow_add" : true,
 *     "allow_remove" : true,
 *     "should_create_template" : false,
 *     "target_element" : {
 *          "composed_object" : "Shop\Entity\ProductPromotion"
 *      }
 * })
 */
private $promotions;

I am using DoctrineModule\Stdlib\Hydrator\DoctrineObject as a hydrator.

Perhaps part of the problem is with the hydrator, because (at least this is how I think it works) it goes through data in $_POST and tries to find matching elements in the form, rather than going through form elements and looking for post data. Either way, if there is no "promotions" key in the post data (e.g. an empty collection), there will be no action taken, thus no way to actually clear the whole collection.

That, however, would cause a different issue, where the entire collection will be wiped if you pass no data to the form, which could happen if you just skip to render the collection for example.

I am nothing more, than really just speculating about how all of this should work, I have no actual idea.

@bakura10
Copy link
Contributor

Hi,

This is more related to the DoctrineModule hydrator. Normally the Doctrine hydrator should call the "removePromotions" method of your entity. Make sure you define it. It's related to the collection strategies: https://github.com/doctrine/DoctrineModule/tree/master/src/DoctrineModule/Stdlib/Hydrator/Strategy

@weierophinney
Copy link
Member

Closing, as it appears this is related to a specific 3rd party hydrator implementation.

@TonyBogdanov TonyBogdanov deleted the patch-1 branch August 26, 2013 11:54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants