我有节点实体。在数据库中,我只有 id 和 title 并且想要生成 URL,我的问题是
- 向实体添加额外的方法是一种好习惯吗?
可以在实体中编写学说查询吗?
$parent = $this->em->getRepository('MyDemoBundle:Nodes')->findOneBy(array("parentId" => $this->getParentId()));
$this->getRequest()->getHost()
可以在使实体 symfony 依赖的实体中使用吗?在 NodeRepository 类中编写 getURL 方法更好吗?
实体中应该有什么,存储库类中应该有什么?
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; } }