在Symfony和Doctrine中,我正在使用会话保存查询结果,然后稍后我用某些参数过滤此结果。问题是,一旦更改了参数,我将无法获得原始结果。这是代码
取得结果并保存到会话
$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(DoctrineORMQuery::HINT_INCLUDE_META_COLUMNS, true);
$this->get('session')->set("results",$query->getResult());
,然后在另一个功能
中过滤结果
$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);
}
}
}
我第一次更改$ minprice和$ maxprice变量,结果是正确的,正是我需要的但是,在再次提高了$ MAXPRICE之后,高价没有被添加到$ FilteredResults
我还尝试将$ uploy和$结果分配给新变量,但仍无法正常工作任何想法如何解决?
ps。我无法更改查询或已经通过查询来获取它们。
当您在会话中保存实体时,该实体将与序列化时刻实际的值序列化。如果您不在会话中重写实体的实体的下一个更改不会影响存储的值。
也有权序列化对private
属性有一些限制。