0

In symfony and Doctrine i am using the session to save a query result then later on i am filtering this result with some parameters. The problem is that once the parameters are changed i can't get the original results set. Here is the code

fetching results and saving to session

$query = $this->getDoctrine()->getManager()->createQueryBuilder();
$query->select("sp")
->from("CoreBundle:ServiceProvider","sp")
->innerJoin("sp.offers","offer")
->innerJoin("offer.service","service","with","offer.service = service")
->innerJoin("sp.firstImage","fi")
->andWhere("sp.city = :city_name")->setParameter("city_name",$cityName)
->orderBy("sp.points" ,"DESC")
->addOrderBy("sp.name" ,"ASC");
$queryAndFilters = $this->getFilters($query,$postData);
$query = $queryAndFilters['query'];
$filters=$queryAndFilters['filters'];
$paginator  = $this->get('knp_paginator');
$pagination = $paginator->paginate(
    $query, /* query NOT result */
    $pageNumber,
    20/*limit per page*/
);

$query = $query->getQuery();
$query->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true);

$this->get('session')->set("results",$query->getResult());

and then in another function

filtering the results

$results = $this->get('session')->get("results");

$filteredResults = array();

// print_r(array_keys($results[0]));
foreach($results as $result){
    $sp = $result;
    foreach($sp->getOffers() as $offer){
        // echo "found offer ".$offer->getName()." with price ".$offer->getPrice()."<br />";
        $sp->removeOffer($offer);
        if($offer->getPrice() < $minPrice || $offer->getPrice() > $maxPrice){

        }else{
            $sp->addOffer($offer);
            // echo 'added $offer '.$offer->getName()." with price : ".$offer->getPrice()."<br />";
        }
        if(sizeof($sp->getOffers()) > 0 ){
            array_push($filteredResults,$sp);
        }
    }
}

the first time i change the $minPrice and $maxPrice variables the results are filtered right and exactly what i need but after raising the $maxPrice again the high prices are not being added to the $filteredResults

i tried also to assign $offer and $result to new variables but still not working any idea how to fix this ?

ps. i can't change the query or to fetch them already with the query.

Community
  • 1
  • 1
Yamen Nassif
  • 2,416
  • 2
  • 24
  • 48

1 Answers1

1

When you save entities in the session this entities are serialized with the values that actual at the serialization moment. Next changes of entities are not affect the stored values if you don't rewrite them in the session.

Also entitiy serializing has some limitations on private properties.

Community
  • 1
  • 1
Timurib
  • 2,735
  • 16
  • 29
  • i dont really understand why serializing would change the results if i DID NOT change them – Yamen Nassif Dec 22 '16 at 13:32
  • as you see in the code the serialized is the getArrayResult() so i serialzed an array not even entities and even though what ever i serialized i did not change them i just added a new array removing from this array is working but readding is not and this is the question – Yamen Nassif Dec 22 '16 at 13:34
  • Try to `unserialize(serialize($query->getResult()))`. – Timurib Dec 22 '16 at 13:40
  • executing this getting me 0 offers in $sp->getOffers(); – Yamen Nassif Dec 22 '16 at 13:45
  • It means that offers just do not serialized. Is relevant property `protected` or `private`? – Timurib Dec 22 '16 at 13:52
  • its 'private' /** * @var ServiceOffer * * @ORM\OneToMany(targetEntity="ServiceOffer", mappedBy="serviceProvider") * @ORM\OrderBy({"points"="DESC"}) */ private $offers; – Yamen Nassif Dec 22 '16 at 13:53
  • > If you intend to serialize (and unserialize) entity instances that still hold references to proxy objects you may run into problems with private properties because of technical limitations. – Timurib Dec 22 '16 at 13:56
  • hmmmm it seems that i need to find another way around, thanks i will mark it as true just for reference – Yamen Nassif Dec 22 '16 at 13:56