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条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历