Last active
July 12, 2017 15:17
-
-
Save gram7gram/639b777bb6a3ddb63cc465a7a2908866 to your computer and use it in GitHub Desktop.
Create comment hierarchy from array
This file contains 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
<?php | |
class Comment | |
{ | |
/** | |
* @var integer | |
*/ | |
private $id; | |
/** | |
* @var \DateTime | |
*/ | |
private $createDatetime; | |
/** | |
* @var string | |
*/ | |
private $value; | |
/** | |
* @var string | |
*/ | |
private $model; | |
/** | |
* @var integer | |
*/ | |
private $modelId; | |
/** | |
* @var array | |
*/ | |
private $children; | |
/** | |
* @var Comment | |
*/ | |
private $parent; | |
/** | |
* Get id | |
* | |
* @return integer | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* Set createDatetime | |
* | |
* @param \DateTime $createDatetime | |
* | |
* @return Comment | |
*/ | |
public function setCreateDatetime($createDatetime) | |
{ | |
$this->createDatetime = $createDatetime; | |
return $this; | |
} | |
/** | |
* Get createDatetime | |
* | |
* @return \DateTime | |
*/ | |
public function getCreateDatetime() | |
{ | |
return $this->createDatetime; | |
} | |
/** | |
* Set value | |
* | |
* @param string $value | |
* | |
* @return Comment | |
*/ | |
public function setValue($value) | |
{ | |
$this->value = $value; | |
return $this; | |
} | |
/** | |
* Get value | |
* | |
* @return string | |
*/ | |
public function getValue() | |
{ | |
return $this->value; | |
} | |
/** | |
* Set model | |
* | |
* @param string $model | |
* | |
* @return Comment | |
*/ | |
public function setModel($model) | |
{ | |
$this->model = $model; | |
return $this; | |
} | |
/** | |
* Get model | |
* | |
* @return string | |
*/ | |
public function getModel() | |
{ | |
return $this->model; | |
} | |
/** | |
* Set modelId | |
* | |
* @param integer $modelId | |
* | |
* @return Comment | |
*/ | |
public function setModelId($modelId) | |
{ | |
$this->modelId = $modelId; | |
return $this; | |
} | |
/** | |
* Get modelId | |
* | |
* @return integer | |
*/ | |
public function getModelId() | |
{ | |
return $this->modelId; | |
} | |
public function addChild(\SD\TaskBundle\Entity\Comment $child) | |
{ | |
$this->children[] = $child; | |
$child->setParent($this); | |
} | |
public function removeChildren(Comment $children) | |
{ | |
$this->children->removeElement($children); | |
} | |
/** | |
* Get children | |
* | |
* @return array | |
*/ | |
public function getChildren() | |
{ | |
return $this->children; | |
} | |
public function setParent(Comment $parent = null) | |
{ | |
$this->parent = $parent; | |
return $this; | |
} | |
/** | |
* Get parent | |
* | |
* @return Comment | |
*/ | |
public function getParent() | |
{ | |
return $this->parent; | |
} | |
} |
This file contains 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
<?php | |
class CommentRepository extends EntityRepository | |
{ | |
public function getTreeStructure($model, $modelId) | |
{ | |
$nodes = $this->findBy([ | |
'model' => $model, | |
'modelId' => $modelId | |
]); | |
$nodesWithoutParent = []; | |
/** @var Comment $node */ | |
foreach ($nodes as &$node) { | |
$node->getChildren()->clear(); | |
if (is_null($node->getParent())) { | |
$nodesWithoutParent[] = $node; | |
} | |
} | |
foreach ($nodes as $key => &$node) { | |
/** @var Comment $parent */ | |
foreach ($nodes as &$parent) { | |
if ($parent === $node->getParent()) { | |
$parent->addChild($node); | |
} | |
} | |
} | |
return $nodesWithoutParent; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment