سؤال

I've recently integrated Doctrine 2 into my ZF app, using the method introduced here:

http://www.zendcasts.com/unit-testing-doctrine-2-entities/2011/02/

I really like the way it works, however I'm confused a little bot on how this will affect the way I used to have my Models and Mappers.

Let me explain the confusion by an example,

Let's say we have User entities and Purchases as in the example given in ZendCast

Right now I have these entities that Doctrine uses

/library/ZC/Entity
    User.php
    Purchase.php

Before I used to have

application/models/
    User.php (Application_Model_User)
    Purchase.php (Application_Model_Purchase)

In classes in application/models/ I used to write functions to act on entities, (Fat model thin controller principle), for example if I wanted to send an email to a user, I would create a method named sendMail in Application_Model_User.

Now I'm not sure if I can add methods to files in /library/ZC/Entity, or if it's a good idea at all since Doctrine uses those files to manage database.

I rather have a separate model file, I also used to have mapper files which worked on more than one Model, for example if I wanted to email all inactive users I would create a method emailInactiveUsers to Application_Model_UserMapper.

How would I do that now?

I also googled a little bit and I found this:

http://net.tutsplus.com/tutorials/php/zend-framework-from-scratch-models-and-integrating-doctrine-orm/

It says

A scaffolding tool, called Doctrine_Cli that creates models from the database very quickly

However the command "generate-models-db" does not exist in my scripts/doctrine.php file. I'm not sure if this is something that Doctrine has stopped supporting in version 2 or what.

هل كانت مفيدة؟

المحلول

Adding methods and properties to your models which are not managed by Doctrine should be no problem. When it comes to mappers, you do not need them with Doctrine. Doctrine already takes care of mappings (e.g. via Annotations in your Entity-class) and for (complex) queries you have the EntityManager/Repositories.

I would place emailInactiveUsers() in a Service, which has access to the EntityManager, e.g.:

class UserMailService
{
    private $em;

    // Inject EntityManager, e.g. via setEntityManager() or __construct()

    public function emailInactiveUsers()
    {
        $mail = new \Zend_Mail();
        $users = $this->em->getRepository('User')->findBy(array('isActive' => false));
        foreach ($users as $user) {
            $mail->addTo($user->getEmail());
        }
        // And so on...
    }
}

Something like sendMail() in my opinion belongs into a Service as it acts on a User-entity and requires a dependency to a Mailer, which should not be coupled with the model.

If a User does something it belongs in the model. If something acts on the User - in your case a Mailer, which takes the email-address from the user and sends out an email - it does not.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top