This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Contents of an ArrayCollection

New topics about Symfony 2 should go here

Contents of an ArrayCollection

by lucuhb » Tue Jun 23, 2015 10:37 am

Hello,

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 :
Code: Select all
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 :
Code: Select all
$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 thise part, when I dump the $pubs variable, I have :
Code: Select all
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() give a true result, so I have nothing in $publications array.
Have I forgotten something ?
lucuhb
Junior Member
 
Posts: 6
Joined: Thu Jul 11, 2013 11:14 am

Re: Contents of an ArrayCollection

by lucuhb » Wed Jun 24, 2015 10:28 am

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 :
Code: Select all
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 :
Code: Select all
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
Code: Select all
UPDATE demand SET pub_refs = REPLACE (pub_refs, "ArrayCollection_elements', 'ArrayCollectionelements')


but this doesn't work because of special chars. Trying with
Code: Select all
UPDATE demand SET pub_refs = REPLACE (pub_refs, "ArrayCollection�_elements', 'ArrayCollection�elements')
doesn't work better. How can I correct this data ?
lucuhb
Junior Member
 
Posts: 6
Joined: Thu Jul 11, 2013 11:14 am