I have node Entity. In database, I have id and title only and want to generate URL, my questions are
- is it good practice to add extra methods to entity ?
-
is it ok to write doctrine query in entity ?
$parent = $this->em->getRepository('MyDemoBundle:Nodes')->findOneBy(array("parentId" => $this->getParentId()));
is it ok to use
$this->getRequest()->getHost()
in entity which makes entity symfony dependent ?Is it better to write getURL method in NodeRepository class ?
-
What should go in entity and what in repository class?
class Node { private $id; private $title; public function getId() { return $this->id; } public function setId($id) { $this->id = $id; } public function getTitle() { return $this->title; } public function setTitle($title) { $this->title = $title; } public function getURL () { if ($this->getType() == "document") { $url = "http://".$this->getRequest()->getHost()."/research/" . preg_replace("/[-\s]+/", "-", strtolower(preg_replace("/[^-a-z0-9\s]+/i", "", trim($this->getTitle())))) . "-" . $this->getId() . "/"; } elseif($this->getType() == "comment") { $parent = $this->em->getRepository('MyDemoBundle:Nodes')->findOneBy(array("parentId" => $this->getParentId())); if($this->getParentType() == "document"){ $url = "http://".$this->getRequest()->getHost()."/research/" . preg_replace("/[-\s]+/", "-", strtolower(preg_replace("/[^-a-z0-9\s]+/i", "", trim($parent->getTitle())))) . "-" . $this->getId(); } else { $url = "http://".$this->getRequest()->getHost()."/content/" . preg_replace("/[-\s]+/", "-", strtolower(preg_replace("/[^-a-z0-9\s]+/i", "", trim($parent->getTitle())))) . "-" . $this->getParentId() ; } } else { $url = "http://".$this->getRequest()->getHost()."/content/" . preg_replace("/[-\s]+/", "-", strtolower(preg_replace("/[^-a-z0-9\s]+/i", "", trim($this->getTitle())))) . "-" . $this->getId() . "/"; } return $url; } }