Symfony2: keep it as function or create Service?

I have this method in my Controller that from indexAction() is called several times. Should I keep it as it is, or should I create Helper Class (Service) for it and other reusable methods, since I could avoid passing arguments such as $em in this case? I can't understand a context of services and when it is comfortable to use them.

public function getProfile($em, $username, $password) {
    $dql = $em->createQuery('SELECT Profiles FROM ProjectProjectBundle:Profiles AS Profiles WHERE Profiles.email = :email AND Profiles.password = :password')
        ->setParameters(array(
                'email' => $username,
                'password' => $password
            ));
    return $dql->getArrayResult();
}

Answers


First of all, know this: You should never, never, never have SQL/DQL in your controllers. Never.

Secondly, you have a few options, but I'm only going to outline one. I'm assuming you have Entities defined so let's start with an options based on that.

  1. Tell doctrine where the Profiles entity's repository is located

    src/Project/ProjectBundle/Entity/Profiles.php

    /**
     * @ORM\Table()
     * @ORM\Entity(repositoryClass="Project\ProjectBundle\Entity\ProfilesRepository")
     */
    class Profiles {}
    
  2. Create the ProfilesRepository class and imlement a custom finder

    src/Project/ProjectBundle/Entity/ProfilesRepository.php

    namespace Project\ProjectBundle\Entity;
    
    use Doctrine\ORM\EntityRepository;
    
    class ProfilesRepository extends EntityRepository
    {
      /**
       * Find a Profiles entity with the given credentials
       *
       * @return \Project\ProjectBundle\Entity\Profiles|null
       */
      public function findByCredentials($username, $password)
      {
        return $this->findBy(array('email' => $username, 'password' => $password ));
      }
    }
    
  3. Update the convenience method in your controller

    /**
     * Fetch a user's profile
     *
     * @return \Project\ProjectBundle\Entity\Profiles
     */
    private function fetchProfile($username, $password)
    {
      try { // Exception-safe code is good!
        $profilesRepo = $this->getDoctrine()->getEntityRepository('ProjectProjectBundle:Profiles');
        return $profilesRepo->findByCredentials($username, $password);
      }
      catch (\Exception $e)
      {
        // Do whatever you want with exceptions
      }
      return null;
    }
    

A few notes on the proposal

  • I ditched the DQL in favor of Repository finders
  • I ditched the array representation of rows in favor of Entity representations. As long as you're actually using Entities, this is preferable.
  • I renamed getProfile() to fetchProfile() since it's a better description for what the method does. I also made it private because that's just good secure coding practice.

Need Your Help

What would be the best way to convert a object in to separate models

jquery loops backbone.js foreach each

I have a json data, which need to convert in to separate key value pair to add in to a array.

How to define global template variable in AngularDart

dart angular-dart

I have simple template with tabs which are switching controlers in template

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.