Last active
September 19, 2017 11:13
-
-
Save suresh-kumara-gist/0f42a1bb6a69afe5e74ab0c92cbda2a7 to your computer and use it in GitHub Desktop.
select queries in drupal 8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // get all Course Ratings. | |
| function getCourseRatings($groupId) { | |
| // Query to get all ratings for the course. | |
| $query = \Drupal::database()->select('attendance__field_course', 'afc'); | |
| $query->join("votingapi_result", "vr", "vr.entity_id = afc.entity_id"); | |
| $query->fields('vr', ['value']); | |
| $query->condition('vr.function', 'vote_sum'); | |
| $query->condition('afc.field_course_target_id', $groupId); | |
| $courseRatings = $query->execute()->fetchAll(\PDO::FETCH_ASSOC); | |
| return $courseRatings; | |
| } | |
| // Sum of votes. | |
| function getSumOfVotes($groupId) { | |
| // Sum of votes | |
| $query = \Drupal::database()->select('attendance__field_course', 'afc'); | |
| $query->join("votingapi_result", "vr", "vr.entity_id = afc.entity_id"); | |
| $query->addExpression("sum(vr.value)", 'sum_votes'); | |
| $query->condition('vr.function', 'vote_sum'); | |
| $query->condition('afc.field_course_target_id', $groupId); | |
| $sumOfVotes = $query->execute()->fetchField(); | |
| return $sumOfVotes; | |
| } | |
| // get all Course Ratings. | |
| function getCourseRatingsStatistics($groupId) { | |
| $query = \Drupal::database()->select('attendance__field_course', 'afc'); | |
| $query->join("votingapi_result", "vr", "vr.entity_id = afc.entity_id"); | |
| $query->fields('vr', ['value']); | |
| $query->condition('vr.function', 'vote_sum'); | |
| $query->condition('afc.field_course_target_id', $groupId); | |
| $query->groupBy("vr.value"); | |
| $query->addExpression("COUNT(*)", 'total_votes'); | |
| $result = $query->execute()->fetchAllKeyed(); | |
| return $result; | |
| } | |
| // fetch single row | |
| $query = \Drupal::database()->select('votingapi_result', 'vr'); | |
| $query->addField('vr', 'value'); | |
| $query->condition('vr.function', 'vote_sum'); | |
| $query->condition('vr.entity_id', $attendanceId); | |
| $ratings = $query->execute()->fetchField(); | |
| return $ratings; | |
| // DB or conditions | |
| $query = \Drupal::database()->select('users_field_data', 'ufd'); | |
| $query->fields('ufd', array('uid', 'name', 'mail', 'access', 'created', 'status')); | |
| /** | |
| * (access not 0 and login not 0 and ufd.created ) or (created < current time - notifytime) | |
| */ | |
| $orandcond1 = db_and()->condition('ufd.access', 0, '!=') | |
| ->condition('ufd.login', 0, '!=') | |
| ->condition('ufd.access', (time() - $notify_time), '<'); | |
| $orandcond2 = db_and()->condition('ufd.login', 0, '=') | |
| ->condition('ufd.created', (time() - $notify_time), '<'); | |
| $condition = db_or()->condition($orandcond1)->condition($orandcond2); | |
| $query->condition($condition); | |
| /** | |
| * Active user and not an admin. | |
| */ | |
| $query->condition('ufd.uid', 1, '!='); | |
| $query->condition('ufd.status', 1, '='); | |
| $result = $query->execute()->fetchAll(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment