Skip to content

Instantly share code, notes, and snippets.

@ucay
Last active March 27, 2018 01:53
Show Gist options
  • Save ucay/f131c582ab6dc0a7cd682a8962c64118 to your computer and use it in GitHub Desktop.
Save ucay/f131c582ab6dc0a7cd682a8962c64118 to your computer and use it in GitHub Desktop.
Symfony 3 Query for family tree ( https://github.com/ErikGartner/dTree )
public function treeAction(Request $request, $id)
{
$tree= $this->get('app.query.tree');
$data = json_encode( $tree->getTreeData($id) );
return new Response($data, 200);
}
<?php
/**
* Class : TreeQuery
* Author : Ulumuddin Cahyadi Yunus <[email protected]>
* On : 9/2/16 9:47 AM
*/
namespace AppBundle\Query;
use Doctrine\ORM\EntityManager;
use Ihsan\AppBundle\Entity\Marriage;
use Ihsan\AppBundle\Entity\Person;
use Ihsan\AppBundle\Entity\Children;
class TreeQuery
{
/**
* @var EntityManager
*/
private $entityManager;
/**
* @var Depth
*/
private $depth;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @param $id
* @param $depth
*
* @return Array
*/
public function getTreeData($id, $depth=10)
{
$this->depth = $depth;
return array(
$this->getPerson($id, 1)
);
}
/**
* @param $id
*
* @return Array
*/
private function getPerson($id, $level=1)
{
$person = $this->entityManager
->getRepository('AppBundle:Person')
->findOneById($id);
$data= array(
"id" => $person->getId(),
"name" => $person->getName(),
"class" => ($person->getIsMale() ? "man" : "woman").($person->getIsDead() ? " rip":""),
"extra" => $this->getExtraData($person),
"level" => $level,
"marriages" => $this->getMarriage($person, $level)
);
return $data;
}
/**
* Get marriages of Person.
*
* @param Person $person
*
* @return Array
*/
private function getMarriage(Person $person, $level = 1)
{
if($person->getIsMale()){
$search = array('husband'=> $person);
}else{
$search = array('wife'=> $person);
}
$marriage = $this->entityManager
->getRepository('AppBundle:Marriage')
->findBy(
$search,
array('date'=>'DESC')
);
$data = array();
foreach($marriage as $mar){
if($person->getIsMale()){
$spouse = $mar->getWife();
}else{
$spouse = $mar->getHusband();
}
$data[]= array(
"spouse" => array(
"id" => $spouse->getId(),
"name" => $spouse->getName(),
"extra" => $this->getExtraData($spouse),
"class" => ($spouse->getIsMale() ? "man" : "woman").($spouse->getIsDead() ? " rip" : ""),
),
"children" => $this->getChildren($mar, $level)
);
}
return $data;
}
/**
* Get childrens of marriage. Thsi will return Array of Person.
*
* @param Marriage $marriage
*
* @return Array
*/
private function getChildren(Marriage $marriage, $level)
{
if($level >= $this->depth) return array();
$children = $this->entityManager
->getRepository('AppBundle:Children')
->findBy(array(
'marriage'=> $marriage)
);
$data = array();
foreach($children as $child){
$data[]= $this->getPerson($child->getChild()->getId(), $level+1);
}
return $data;
}
/**
* Get extra data from Person, such as birth, address, picture, etc.
*
* @param Person $person
*
* @return Array
*/
private function getExtraData(Person $person)
{
$data = array(
"id" => $person->getId(),
"birth_date" => $person->getBirthDate(),
"birth_year" => $person->getBirthYear(),
"death_date" => $person->getDeathDate(),
"death_year" => $person->getDeathYear(),
);
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment