0

So I have a database setup like this.

enter image description here

Phone numbers belong to groupings. And users belong to groupings as well. I'm trying to figure out how to get all users that belong to a grouping but through the entity object instead of just a query if this is possible.

For example I'm aware I could do a query like this...

<?php

/**
 * Auto generated by MySQL Workbench Schema Exporter.
 * Version 3.0.3 (doctrine2-annotation) on 2017-03-27 04:09:37.
 * Goto https://github.com/johmue/mysql-workbench-schema-exporter for more
 * information.
 */

namespace Entity;

use Doctrine\ORM\Mapping as ORM;
use Entity\BaseGrouping;

/**
 * Entity\Grouping
 *
 * @ORM\Entity()
 */
class Grouping extends BaseGrouping
{
    public function getUsersByPriority(){
        global $entityManager;

        $users = $entityManager->getRepository('Entity\User')->findBy(array(),array('priority' => 'ASC'));

        return $users;
    }
}

With a little modification I could add another filter perhaps so that only the correct results pertaining to that group are shown instead of everything. Right now this will result in just every user showing instead of those that should belong to just the group.

What I'm looking for is something kind of like this...

$results = $entityManager->getRepository('entity\Phonenumber')->findBy(array('number' => '+'.$numberCalled));

            if(count($results)<=0 || count($results)>1){
                sendEmail('Error Occured', 'There was duplicate phone numbers in the database, used fallbacknumber<br/><br/>'.print_r($_REQUEST,true),"joe@poolserviceusa.com");
                return $fallbacknumber;
            }else{
                $phonenumber = $results[0];
                $group = $phonenumber->getGrouping(); //@JA - Returns the group object and stores it to variable group in scope
            }

            //Get list of all users in the group
            $users = $group->getUsersByPriority(); //@JA - Returns all users associated with the group

            //Find the first active and not busy user
            foreach($users as $user){
                echo '<test>'.$user->getUserName().'<test>';
            }

Since I mapped correctly all the doctrine classes I'm able to just say $phonenumber->getGroupings(); and it returns me only the groupings that belong to that phonenumber which is perfect!

What I need now however is all the users that belong to that particular group?

Easy enough if we do $group->getUsers(); The problem here is I need the users sorted by priority and there is no sorting when I use these default methods.

How do I get all the users of just the group while sorting by priority?

Joseph Astrahan
  • 8,659
  • 12
  • 83
  • 154

1 Answers1

0

I think I found the answer but I don't know if this is the best answer or not. I modified the function getUsersByPriority to this.

public function getUsersByPriority(){
        global $entityManager;

        $grouping_id = $this->getId();

        $users = $entityManager->getRepository('Entity\User')->findBy(array('grouping_id' => $grouping_id),array('priority' => 'ASC'));

        //@JA - Get reference to the users of just this grouping.
        $users = $this->users;

        return $users;
    }

I didn't realize I could use $this->getId(); to get reference to the current instance in this case.

Joseph Astrahan
  • 8,659
  • 12
  • 83
  • 154