Skip to content

Instantly share code, notes, and snippets.

@Erikvv
Created October 15, 2014 12:15
Show Gist options
  • Save Erikvv/9b9821945c9bb1fd01e6 to your computer and use it in GitHub Desktop.
Save Erikvv/9b9821945c9bb1fd01e6 to your computer and use it in GitHub Desktop.
<?php
namespace Placement\Service;
use Orm\Entity\Placement;
use Orm\Entity\Student;
use Orm\Entity\Employee;
use Orm\Entity\PersonPlacement;
class PersonHelper
extends \ZF2Core\Service\AbstractService
{
/* \Orm\Entity\Placment */
private $placement;
public function __construct(Placement $placement)
{
$this->placement = $placement;
}
public function getPersonHelperForStudent()
{
$student = $this->getStudent();
if ($student === null)
{
return null;
}
var_dump($student->getPerson()); exit;
return $this->getServiceManager()->get('Person\Service\PersonHelper')->__invoke($student->getPerson());
}
/**
* @return Student
*
* @TODO return array for multiple students?
*/
public function getStudent()
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('s')
->from('Orm\Entity\Student', 's')
->innerJoin('s.person', 'person')
->innerJoin('person.personPlacementCollection', 'pp')
->where('pp.placement = :placement')
->setParameter('placement', $this->placement);
$result = $qb->getQuery()->execute();
if( count($result) === 1 )
{
return $result[0];
}
else
{
return null;
}
}
/**
* @return Employee
*/
public function getEmployee()
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('e')
->from('Orm\Entity\Employee', 'e')
->innerJoin('e.person', 'person')
->innerJoin('person.personPlacementCollection', 'pp')
->where('pp.placement = :placement')
->setParameter('placement', $this->placement);
$result = $qb->getQuery()->execute();
if( count($result) === 1 )
{
return $result[0];
}
else
{
return null;
}
}
/**
* @return PersonPlacement[]
*/
public function categorizePersons()
{
$students = $this->associatedStudents();
$employees = $this->associatedEmployees();
$external = $this->associatedExternalPersons();
$everyone = $this->placement->getPersonPlacementCollection()->toArray();
$remainder = array_udiff($everyone, $students, $employees, $external, ['Orm\Entity\PersonPlacement', 'equals']);
return [
'count' => count($everyone),
'students' => $students,
'employees' => $employees,
'external' => $external,
'remainder' => $remainder,
];
}
/**
* @return array
*/
public function associatedStudents()
{
$qb = $this->getEntityManager()->getRepository('Orm\Entity\PersonPlacement')->createQueryBuilder('pp');
$qb->select('pp');
$qb->innerJoin('pp.placement', 'placement');
$qb->innerJoin('pp.person', 'person');
$qb->innerJoin('person.student', 'student');
$qb->where('placement = :arg');
$qb->setParameter("arg", $this->placement);
return $qb->getQuery()->getResult();
}
/**
* @return array
*/
public function associatedEmployees()
{
$qb = $this->getEntityManager()->getRepository('Orm\Entity\PersonPlacement')->createQueryBuilder('pp');
$qb->select('pp');
$qb->innerJoin('pp.placement', 'placement');
$qb->innerJoin('pp.person', 'person');
$qb->innerJoin('person.employee', 'employee');
$qb->where('placement = :arg');
$qb->setParameter("arg", $this->placement);
return $qb->getQuery()->getResult();
}
/**
* @return array
*/
public function associatedExternalPersons()
{
$qb = $this->getEntityManager()->getRepository('Orm\Entity\PersonPlacement')->createQueryBuilder('pp');
$qb->select('pp');
$qb->innerJoin('pp.placement', 'placement');
$qb->innerJoin('pp.person', 'person');
$qb->innerJoin('person.organisationPersonCollection', 'opc');
$qb->where('placement = :arg');
$qb->setParameter("arg", $this->placement);
return $qb->getQuery()->getResult();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment