Pergunta

Consider that I want to be able to save a user to the database, my add action is as follow:

    public function addAction()
    {

    $form = new UserForm();
    $form->get('submit')->setValue('Add');

    $request = $this->getRequest();

    if ($request->isPost()) {

        $userFilter = new UserFilter();
        $form->setInputFilter( $userFilter->getInputFilter() );
        $form->setData( $request->getPost() );

        if ($form->isValid())

            $user = new User();
            $user->setEmail($form->getInputFilter()->getValue('email')  );
            $user->setNome( $form->getInputFilter()->getValue('name') );


            $em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
            $em->persist($user);
            $em->flush();


            return $this->redirect()->toRoute('user');

        }
    }

    return array('form' => $form);
    }

Very easy to save a user to the databse, however if I need to add some complex business logic, let’s i want to check if email is unique and that i also want to access some web service to check is answer to the ultimate question of life, the universe, and everything is really 42 and if is true i want to save the user to the database if not i want to show a message to the user.

It can be done add action but as i am told this is not a very good practice, i can put this business logic inside the entity User, but this would add coupling between the zf2 and doctrine with the entity and this is also bad. Searching the web for a solution the answer seems to be put the business logic in Service Layer.

Fallowing the Service Layer solution, one would create a class UserBusinessLogic and create a method save that would perform the business logic and save the user if everything is ok.

Does this sound right? Is there any documentation on the subject? Perhaps a code example showing how to deal with business logic using doctrine 2 and zf2 and services.

I guess bottom line is: What is the best practice to where to put the business logic when using zf2 and doctrine 2?

Assuming that the service solution is the best way to go. If I have Entitys Users, Groups and the relation between those two I would create a Service called “access” and this service would be the one who receives data from controllers to save users an groups, link those 2 and also perform any other task like sending mail to reset a user password. Does this sound right?

Foi útil?

Solução

You have the right idea. To de-couple Doctrine 2 more you can create another layer that follows one of the interfaces in Zend\Db, but uses Doctrine to accomplish the database interaction.

Also, for verification you can create custom input filters for the form that check against the database using Doctrine.

The idea is that whatever is behind the service can be replaced by changing the service as long as the method names remain the same. This way you could later replace Doctrine with Propel, for example, and you wouldn't have to refactor your controllers & views, just the service class.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top