doulu1914 2013-07-09 08:59
浏览 26
已采纳

在Symfony2中的实体类中添加额外的方法是一种好习惯吗?

I have node Entity. In database, I have id and title only and want to generate URL, my questions are

  1. is it good practice to add extra methods to entity ?
  2. is it ok to write doctrine query in entity ?

    $parent = $this->em->getRepository('MyDemoBundle:Nodes')->findOneBy(array("parentId" => $this->getParentId()));
    
  3. is it ok to use $this->getRequest()->getHost() in entity which makes entity symfony dependent ?

  4. Is it better to write getURL method in NodeRepository class ?

  5. 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;
       }
    
    }
    
  • 写回答

2条回答 默认 最新

  • dte66654 2013-07-09 09:12
    关注

    The idea is bad. And besides this, you have no access to the request or the entity manager within the entity. A entity is a POPO (Plain old PHP object), i.e. a dummy only representing data.

    If you want to get the parent with a getter, then should you notate a field with the right annotations/mappings (OneToMany, ManyToOne, ManyToMany). Read a little bit about in the documentation. With them you don't need to write any queries. Other queries go into the repository class.

    And you should read a little bit about, how urls are generated in symfony. You don't need to refer to the host. a URL is mostly generated in the controller or the template you needs them to output.

    btw. if you want slugged titles for urls you should take a look at the sluggable behavior of the doctrine extensions (installable with DoctrineExtensionsBundle)

    All in all, you should read the entire book to learn the basics!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集