Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[PropertyAccess] Fails with poorly indexed arrays #10099

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

Closed
LewisW opened this issue Jan 21, 2014 · 4 comments
Closed

[PropertyAccess] Fails with poorly indexed arrays #10099

LewisW opened this issue Jan 21, 2014 · 4 comments

Comments

@LewisW
Copy link

LewisW commented Jan 21, 2014

Lets say I'm submitting a form, built from a collection, with just the following form information:

form[main][1][test1]: 10
form[main][1][test2]: test

The PropertyAccess bundle will try and access this property, but detects form[main][1] as a numeric index. Then when calling PropertyAccessor::getValue, it tries to iterate through all the properties of 'main' as if it was a zero indexed array. This means it will try and access form[main][0], even though it does not exist. PropertyAccessory::readPropertiesUntil with then create form[main][0] which will fail any NotBlank validations later on - even though it was never submitted in the original form.

This might sound like it's easily fixable by just ensuring that the form always submits properly indexed arrays. However this isn't the case with most Javascript solutions used for collections - they can easily create broken indexes when removing collection items in the middle of other collection items.

If none of this makes sense, please let me know and I can explain with some proof of concept code.

@lavoiesl
Copy link
Contributor

This is not necessarily a poorly indexed array, the key could be representing an id, and thus be perfectly legitimate. This main source of the problem seems to be that readPropertiesUntil is creating indices while it is simply trying to read. This is inconsistent with PHP's behaviour.

However, getValue does not have its argument $objectOrArray passed as a reference, the original array should not be changed. You should only see the problem arise if you are using objects.

Plus, this whole concept seems rather inefficient. It means that if I ask for index 1000, it will create a thousand empty items just to later call the last item and trash the rest.

@LewisW
Copy link
Author

LewisW commented Jan 24, 2014

You're right, a poorly indexed array isn't the best way to describe it, I was strapped for time and couldn't think of a way of describing them concisely (I guess non-consecutive would be better).

I agree it seems like odd behaviour, and yes because we're using Doctrine it is actually performing this on a PersistentCollection and hence it is actually creating these empty indexes.

I don't think it wouldn't even get to index 1,000 - it seems to limit the amount of properties it will read based on the amount of properties in the array. So it would iterate once, try and access index 0, fail and create that index, and then stop. When I get time I will put together some tests to try this theory.

@Tobion
Copy link
Contributor

Tobion commented Mar 4, 2015

Then when calling PropertyAccessor::getValue, it tries to iterate through all the properties of 'main' as if it was a zero indexed array.

This is not true. PropertyAccessor only accessed/creates array indexes that are explicitly requested.
So if the zero-indexes element is requested, then by the form component. Maybe by the resizing logic of the collection. So the fault is definitely not in the PropertyAccessor which makes the ticket confusing. Please create a new ticket if you can provide reproducible code. Then we can look where the error actually lies.

@Tobion Tobion closed this as completed Mar 4, 2015
@Tobion
Copy link
Contributor

Tobion commented Mar 4, 2015

Btw, seems related to #7828

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

3 participants