views:

32

answers:

3

i am using zend framework 1.10 with doctrine 2. i wonder if in my (doctrine) model class, isit a good idea to reference a variable set by my application (bootstrap.php, variable stored in Zend_Registry, i think its something like a global variable)

what i want to access is the doctrine entityManager. also i want the id of the logged in user

A: 

you should simply use Zend_Auth for the logged-in-userId problem, then could do something like the following in your model

class Model extends BaseModel
{
    public function something()
    {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {

            $loggedInUserId = $auth->getIdentity()->id;
        }
    }
}
zolex
yes i did something like that currently. i was wondering if its better to have some separation where i dont reference Zend_Auth from my model? or is that too much?
jiewmeng
A: 

There is nothing wrong with this approach (unless you are referring to singletons). Use dependency injection where possible.

However I'd create a service (or two) for this.

class Modulename_Services_Servicename
{
    public function getCurrentUser() { ... }

    public function getCurrentUserModel() { ... }

    public function isLogged() { ... }

    public function authenticate() { ... }

    public function getSomeData()
    {
        $user = $this->getCurrentUser()
        $model = new YourModel($user);
        $query = ....;
        $result = $query->execute();

        return $result;

    }

    public function getSomeMoreData($usermodel) { ... }

}
takeshin
`Zend_Registry` is a singleton. but what is the problem with a singleton over any kind of variable? but what u are doing seems like mixing authentication and model logic?
jiewmeng
@jiewmeng Singletons are like constants. You may get it anywhere. For other variables dependency injection is the way. As I wrote, I would create service (*or two*, separate for auth and separate for the other logic.)
takeshin
+1  A: 

I am building a project with similar setup (ZF 1.10 + Doctrine2) and I've used dependency injection to deal with this situation, much like takeshin said. Here goes full project repository URL: https://bitbucket.org/phpfour/zf-doctrine2. Below are some code excerpts.

Here's my controller:

<?php

require_once APPLICATION_PATH . "/models/PostManager.php";

class IndexController extends Zend_Controller_Action
{
    private $_em;

    public function init()
    {
        $this->_em = $this->getInvokeArg('bootstrap')->getResource('doctrine');
    }

    public function indexAction()
    {
        $pm = new PostManager($this->_em);
        $this->view->posts = $pm->getPublicPosts();
    }

My entity manager (or service class):

<?php

class PostManager
{
    protected $_em;

    public function __construct(Doctrine\ORM\EntityManager $em)
    {
        $this->_em = $em;
    }

    public function getPublicPosts()
    {
        $query = $this->_em->createQuery('SELECT p FROM Entities\Post p WHERE p.visible = true');
        $posts = $query->getResult();
        return $posts;
    }

Hope this helps!

phpfour