Get content of an ArrayCollection

4.6k views Asked by At

I would like to upgrade my symfony 2 project from 2.3 to 2.7 LTS version. I have a problem in a repository to get result of a query. In 2.3, this query give me something :

    public function findProtectedPublications( $steps, $start, $end)
    {

        $query= $this->getEntityManager()
        ->createQueryBuilder()
        ->select('d.pubRefs')
        ->from('ImpressionDemandBundle:Event', 'h')
        ->innerJoin('h.demand','d')
        ->where('d.protectedPublications = :pub')
        ->setParameter('pub', 1 )   
        ->andWhere('h.date >= :start')
        ->setParameter('start', $start )
        ->andWhere('h.date <= :end')
        ->setParameter('end', $end )
        ->andWhere('h.stepId in (:steps)')
        ->setParameter('steps', $steps )
        ->orderBy('d.id','ASC')
        ->getQuery();

        $results = $query->getResult();
        $publications = array();
        if ($results && ! empty ($results)){
            foreach($results as $result){
                $pubs = $result['pubRefs'];
                if ($pubs && ! empty($pubs)){
                    foreach($pubs as $pub){
                        $publications[] = $pub;
                    }
                }
            }
        }

        return  $publications;

    }

But this code doesn't work in earlier version because $pubs variable in an ArrayCollection. So I changed the end of my code with this :

    $results = $query->getResult();           
        $publications = array();
        if ($results && ! empty ($results)){
                    foreach($results as $result){
                        $pubs = $result['pubRefs'];
                        var_dump($pubs);
                        if (! $pubs->isEmpty()){
                            $arrayPubs = $pubs->toArray();
                            foreach($arrayPubs as $pub){
                $publications[] = $pub;
                            }
                        }
                    }
        }

        return  $publications;

In this part, when I dump the $pubs variable, I have :

    object(Doctrine\Common\Collections\ArrayCollection)#131 (2) {
  ["elements":"Doctrine\Common\Collections\ArrayCollection":private]=>
  NULL
  ["_elements":"Doctrine\Common\Collections\ArrayCollection":private]=>
  array(1) {
    [0]=>
    object(Impression\DemandBundle\Entity\Publication)#125 (5) {
      ["editor":"Impression\DemandBundle\Entity\Publication":private]=>
      string(24) "Journal Le Monde 4-10-13"
      ["coauthors":"Impression\DemandBundle\Entity\Publication":private]=>
      string(12) "Machin Machin"
      ["title":"Impression\DemandBundle\Entity\Publication":private]=>
      string(57) "La tragédie de Lampedusa: s"émouvoir, comprendre, agir."
      ["nbPages":"Impression\DemandBundle\Entity\Publication":private]=>
      float(1)
      ["nbCopies":"Impression\DemandBundle\Entity\Publication":private]=>
      float(40)
    }
  }
}

So it seems that there are elements in this ArrayCollection, but the test $pubs->isEmpty() gives a true result, so I have nothing in $publications array.

Edit: In fact, the problem seems to be due to my data in the database : for an object previous from my upgrade, I have something like this in the database :

O:43:"Doctrine\Common\Collections\ArrayCollection":1:{s:54:"Doctrine\Common\Collections\ArrayCollection_elements";a:1:{i:0;O:42:"Impression\DemandBundle\Entity\Publication":5:{s:50:"Impression\DemandBundle\Entity\Publicationeditor";s:5:"BREAL";s:53:"Impression\DemandBundle\Entity\Publicationcoauthors";s:5:"MONOT";s:49:"Impression\DemandBundle\Entity\Publicationtitle";s:18:"USA Canada mexique";s:51:"Impression\DemandBundle\Entity\PublicationnbPages";d:150;s:52:"Impression\DemandBundle\Entity\PublicationnbCopies";d:150;}}}

and this gives the error. For a object add after my upgrade, I have something like this in the database :

O:43:"Doctrine\Common\Collections\ArrayCollection":1:{s:53:"Doctrine\Common\Collections\ArrayCollectionelements";a:1:{i:0;O:42:"Impression\DemandBundle\Entity\Publication":5:{s:50:"Impression\DemandBundle\Entity\Publicationeditor";s:8:"dfg dfgd";s:53:"Impression\DemandBundle\Entity\Publicationcoauthors";s:7:"dfg dfg";s:49:"Impression\DemandBundle\Entity\Publicationtitle";s:5:"fdg d";s:51:"Impression\DemandBundle\Entity\PublicationnbPages";d:5;s:52:"Impression\DemandBundle\Entity\PublicationnbCopies";d:3;}}}

and the function findProtectedPublications() works without errors.

The difference between the two versions is ArrayCollection_elements for the first and ArrayCollectionelements for the second.

To correct this data, I tried with

UPDATE demand SET pub_refs = REPLACE (pub_refs, "ArrayCollection_elements', 'ArrayCollectionelements')

but this doesn't work because of special chars. Trying with

UPDATE demand SET pub_refs = REPLACE (pub_refs, "ArrayCollection�_elements', 'ArrayCollection�elements')

doesn't work better. How can I correct this data ?

2

There are 2 answers

1
Maltronic On

Doctrine can populate results as an Array instead of an ArrayCollection, simply change the getResult() call to:

 $results = $query->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);

This would be the most efficient way to complete your task however you could also use ArrayCollection's built-in toArray() method to convert its own data to array format:

 $publications = $results->toArray();
0
Lucuhb On

As the problem seems to be due to a change in the storage of ArrayCollection in database between 2.3 and 2.7 version of symfony, I created an line command to update these in database.