Skip to content

Instantly share code, notes, and snippets.

@bolbo
Last active August 29, 2015 14:25
Show Gist options
  • Save bolbo/0eea9640a5b29c5c8d38 to your computer and use it in GitHub Desktop.
Save bolbo/0eea9640a5b29c5c8d38 to your computer and use it in GitHub Desktop.
/**
* @param $where
* @return \PommProject\Foundation\ResultIterator
*/
public function findWithModeleAndEquipements($where)
{
$sql = <<<SQL
WITH equipement_detail AS (
SELECT :projectionEquipement
FROM :vehiculeEquipement mfe
LEFT JOIN :equipement USING (equipement_id)
)
SELECT
:projection
FROM
:vehicule v
LEFT JOIN :marque ON v.marque_id = marque.id
LEFT JOIN equipement_detail USING (modele_id, finition_id)
WHERE :condition
GROUP BY v.vehicule_id, marque.*
SQL;
$projectionEquipement = $this
->getSession()
->getModel('\My\Component\Model\Project\ConfiguratorSchema\vehiculeEquipementModel')
->createProjection()
->setField('equipement_label', 'equipement.label', 'varchar')
;
$projection = $this->createProjection()
->setField('marque', 'marque', 'public.marque')
->setField('equipements', 'array_agg(equipement_detail)', 'configurator.modele_equipement[]');
$sql = strtr($sql,
[
':projection' => $projection->formatFieldsWithFieldAlias('v'),
':vehicule' => $this->getStructure()->getRelation(),
':marque' => $this
->getSession()
->getModel('\My\Component\Model\Project\PublicSchema\MarqueModel')
->getStructure()
->getRelation(),
':projectionEquipement' => $projectionEquipement->formatFieldsWithFieldAlias('mfe'),
':equipement' => $this
->getSession()
->getModel('\My\Component\Model\Project\ConfiguratorSchema\EquipementModel')
->getStructure()
->getRelation(),
':condition' => $where,
]
);
return $this->query($sql, $where->getValues(), $projection);
}
@ishenkoyv
Copy link

Is there any reason to use projections instead of raw sql? Do you have a lot of 'custom' fields in the model?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment