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 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题