Skip to content

Instantly share code, notes, and snippets.

@psaunders88
Last active December 31, 2015 05:49
Show Gist options
  • Save psaunders88/7943304 to your computer and use it in GitHub Desktop.
Save psaunders88/7943304 to your computer and use it in GitHub Desktop.
A simple example of a function that gathers data from a doctrine and makes use of the join. Nothing special just good for reference
/**
* Function to select particpants by their particiation
*
* @param integer $statusId Status id of partipation
* @param integer $value Value for query
* @param string $operator Operator for claus opions: moreThan, lessThan, atLeast, equals
*
* @return array
*/
public function selectUsersByParticipation($statusId, $value, $operator)
{
$parameters = array(
'attendanceStatusId' => $statusId,
'value' => $value
);
switch ($operator) {
case "moreThan":
$operator = '>';
break;
case "lessThan":
$operator = '<';
break;
case "atLeast":
$operator = '>=';
break;
case "equals":
$operator = '=';
break;
}
// The HIDDEN keyword takes the value out of the results
// This which makes for easier use of the results (unless you need to the count in the results)
$dql = 'SELECT p, COUNT(t.participantId) HIDDEN number
FROM Library\Entity\Participant p
LEFT JOIN p.surveyParticipants t
WHERE t.attendanceStatusId = :attendanceStatusId
GROUP BY t.participantId
HAVING number '.$operator.' :value';
$query = $this->_em->createQuery($dql)->setParameters($parameters);
$result = $query->getResult();
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment