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

在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;
       }
    
    }
    

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!

You might also need to read about How to create custom repository classses This is the right way to add "custom methods" tp entity classes in symfony

Here is the introduction of the article:

In the previous sections, you began constructing and using more complex queries from inside a controller. In order to isolate, reuse and test these queries, it's a good practice to create a custom repository class for your entity. Methods containing your query logic can then be stored in this class.

To do this, add the repository class name to your entity's mapping definition: