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

fileprg and array merge recursive #4974

Closed
davidnjuko opened this issue Aug 16, 2013 · 10 comments
Closed

fileprg and array merge recursive #4974

davidnjuko opened this issue Aug 16, 2013 · 10 comments
Labels

Comments

@davidnjuko
Copy link

In HandlePostRequest method of Zend/Mvc/Controller/Plugin/FilePostRedirectGet.php, you're using array_merge_recursive to compile file upload data, and field input data in order to validate the form. But in some cases it can causes issue, i'm using Doctrine2 and object are created thanks to an hydrator based on collection of my form

Here is my files array

array (size=2)
  'permission_slip' => 
    array (size=5)
      'name' => string '' (length=0)
      'type' => string '' (length=0)
      'tmp_name' => string '' (length=0)
      'error' => int 4
      'size' => int 0
  'answers' => 
    array (size=1)
      224 => 
        array (size=1)
          'value' => 
            array (size=5)
              'name' => string 'coteTissus.png' (length=14)
              'type' => string 'image/png' (length=9)
              'tmp_name' => string '/usr/local/zend/tmp/phpI2s8zJ' (length=29)
              'error' => int 0
              'size' => int 192191

And my input array

array (size=6)
  'firstname' => string '' (length=0)
  'lastname' => string '' (length=0)
  'birthdate' => string '' (length=0)
  'email' => string '' (length=0)
  'answers' => 
    array (size=1)
      224 => 
        array (size=2)
          'field' => string '224' (length=3)
          'id' => string '' (length=0)
  'submit_pay' => string 'Etape suivante' (length=14)

array_merge_recursive on those arrays produce this array :

array (size=7)
  'firstname' => string '' (length=0)
  'lastname' => string '' (length=0)
  'birthdate' => string '' (length=0)
  'email' => string '' (length=0)
  'answers' => 
    array (size=2)
      224 => 
        array (size=2)
          'field' => string '224' (length=3)
          'id' => string '' (length=0)
      225 => 
        array (size=1)
          'value' => 
            array (size=5)
              'name' => string 'coteTissus.png' (length=14)
              'type' => string 'image/png' (length=9)
              'tmp_name' => string '/usr/local/zend/tmp/phpI2s8zJ' (length=29)
              'error' => int 0
              'size' => int 192191
  'submit_pay' => string 'Etape suivante' (length=14)
  'permission_slip' => 
    array (size=5)
      'name' => string '' (length=0)
      'type' => string '' (length=0)
      'tmp_name' => string '' (length=0)
      'error' => int 4
      'size' => int 0

As you can see it created a new key 225 (so for doctrine, a new object)

I fixed it Extending the class and using the function to merge arrays, but i'm sure there is a better way to do it :

function array_merge_recursive_distinct ( array &$array1, array &$array2 )
{
    $merged = $array1;

    foreach ( $array2 as $key => &$value )
    {
        if ( is_array ( $value ) && isset ( $merged [$key] ) && is_array ( $merged [$key] ) )
        {
            $merged [$key] = array_merge_recursive_distinct ( $merged [$key], $value );
        }
        else
        {
            $merged [$key] = $value;
        }
    }

    return $merged;
}
@AydinHassan
Copy link
Contributor

Not 100% if it's the same issue - but I remember running into something similar - my solution was to use array_replace_recursive() instead of array_merge_recursive().

Again, it might not be the same issue, I will double check when I get a chance tomorrow

@Thinkscape
Copy link
Member

Take a look at ArrayUtils::merge()

@weierophinney
Copy link
Member

I'm having trouble determining if this is something that needs to be fixed in the framework, or that can be fixed in userland using ArrayUtils::merge() (per the suggestion of @Thinkscape); can somebody clarify?

@2DivisionsByZero
Copy link
Contributor

Collections with more than one element are a big problem without this fix.

@2DivisionsByZero
Copy link
Contributor

Did some proving tests: 2DivisionsByZero/zf2@14aaa1f

@2DivisionsByZero
Copy link
Contributor

@weierophinney Did some proving tests: 2DivisionsByZero/zf2@14aaa1f

@2DivisionsByZero
Copy link
Contributor

Previous commit "Testcase for successive file uploads" is required too in

@2DivisionsByZero
Copy link
Contributor

@dpommeranz Could you take a look on the test? https://github.com/2DivisionsByZero/zf2/tree/fileprg-tests

@weierophinney
Copy link
Member

@dpommeranz Can you submit d05f1a7 as a PR against this repository, please? and/or @2DivisionsByZero you could submit your tests as a PR. Either way, the work is being done -- a PR is the correct way to get it into the repository.

stefanotorresi pushed a commit to stefanotorresi/zf2 that referenced this issue Jan 14, 2014
Conflicts:
	library/Zend/Mvc/Controller/Plugin/FilePostRedirectGet.php
@weierophinney
Copy link
Member

Fixed with #5244

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

Successfully merging a pull request may close this issue.

5 participants