doctrine2 - Symfony Doctrine Hydrator - With custom hydrator I lose my ManyToOne relation -
i extends objecthydrator benefit of hydration of manytoone relation , add field entity.
here hydrator: statisticsdatahydrator.php
namespace appbundle\hydrator\projectassignment; use appbundle\entity\projectassignment; use doctrine\orm\internal\hydration\objecthydrator; class statisticsdatahydrator extends objecthydrator { /** * {@inheritdoc} */ protected function hydraterowdata(array $data, array &$result) { $hydrated_result = array(); parent::hydraterowdata($data, $hydrated_result); /** @var projectassignment $project_assignment */ $project_assignment = $hydrated_result[0][0]; $result[] = $project_assignment; } }
here config: config.yml
doctrine: orm: hydrators: project_assignment_statisticsdata_hydrator: appbundle\hydrator\projectassignment\statisticsdatahydrator
where don't use hydrator have no problem:
/** * @param projectstage $stage * @return array */ public function findbystagewithstatisticsdata(projectstage $stage){ $qb = $this->createquerybuilder('pa'); $qb //->addselect('44') ->where($qb->expr()->eq('pa.project_stage', ':stage')) ->setparameter('stage', $stage); return $qb->getquery()->getresult(); }
but when use hydrator:
/** * @param projectstage $stage * @return array */ public function findbystagewithstatisticsdata(projectstage $stage){ $qb = $this->createquerybuilder('pa'); $qb ->addselect('1234') // referencial value ->where($qb->expr()->eq('pa.project_stage', ':stage')) ->setparameter('stage', $stage); return $qb->getquery()->getresult('project_assignment_statisticsdata_hydrator'); }
the strangest behavior same occure config: config.yml
doctrine: orm: hydrators: project_assignment_statisticsdata_hydrator: doctrine\orm\internal\hydration\objecthydrator
i have tried kind of fetch on relation no success:
@orm\manytoone(... , fetch="eager") @orm\manytoone(... , fetch="lazy") ...
maybe have use proxy on entity, don't know :(
thank help!
great! found problem, query builder. had manually add joins , select of related objects.
/** * @param projectstage $stage * @return array */ public function findbystagewithstatisticsdata(projectstage $stage){ $qb = $this->createquerybuilder('pa'); $qb ->addselect('e') // added ->addselect('r') // added ->addselect('1234') ->leftjoin('pa.employee', 'e') // added ->leftjoin('pa.role', 'r') // added ->where($qb->expr()->eq('pa.project_stage', ':stage')) ->setparameter('stage', $stage); return $qb->getquery()->getresult('project_assignment_statisticsdata_hydrator'); }
bonus, here hydrator (it can someone):
namespace appbundle\hydrator\projectassignment; use appbundle\entity\hydrator\projectassignment\statisticsdata; use appbundle\entity\projectassignment; use doctrine\orm\internal\hydration\objecthydrator; class statisticsdatahydrator extends objecthydrator { /** * {@inheritdoc} */ protected function hydraterowdata(array $data, array &$result) { $hydrated_result = array(); parent::hydraterowdata($data, $hydrated_result); /** @var projectassignment $project_assignment */ $project_assignment = $hydrated_result[0][0]; $keys = array_keys($hydrated_result); $key = end($keys); $statistics_data = new statisticsdata($project_assignment); $statistics_data->settotalworkedtime((int)$hydrated_result[$key][1]); $project_assignment->setstatisticsdata($statistics_data); $result[] = $project_assignment; } }
in entity have folowing attribute/getter/setter
/********** non synced fields **********/ /** @var statisticsdata $statistics_data */ private $statistics_data; /** * @return statisticsdata */ public function getstatisticsdata() { return $this->statistics_data; } /** * @param statisticsdata $statistics_data */ public function setstatisticsdata($statistics_data) { $this->statistics_data = $statistics_data; } /***************************************/
Comments
Post a Comment