Jump to content

Recommended Posts

Currently my models all derive from a base Model class which instantiates the database adapter when it gets instantiated. This works just fine but the problem I have with it is that each instance of the model has its own reference to the database adapter. Even making it static, the models will all have a reference to the adapter, it'll just be the same one. I was thinking about loading the database adapter at the application level and passing it into the model when it gets instantiated but this doesn't really seem to fix the issue since each instance of the model will still have a reference to the adapter.

 

This may not be a big deal at all but it is definitely a lot more ugly when I print_r the model. Any suggestions on ways around this? I'm not completely familiar how other frameworks handle this so I'm not sure where they're handling their database.

 

**edit** Apparently Zend uses a mapper to map the model to the database. I'll ponder that for a bit and see if that's something I can do as well. I'd still appreciate any feedback if anyone feels obliged.

Link to comment
https://forums.phpfreaks.com/topic/244609-databases-and-models/
Share on other sites

The reason being that you shouldn't have a Model base class to begin with... The models that require a database connection can retrieve/make one when needed and those that don't, just don't.

 

class User
{
    public function __construct(Zend_Db_Adapter_Abstract $adapter)
    {
        $this->adapter = $adapter;
    }
}

 

class ContactEmail
{
    public function __construct($charset = 'UTF-8') {
        $this->emailer = new Zend_Mail($charset);
    }
}

 

2 model's and only tied to the components they actually need.

I noticed that since all the models I have need access to the database so rather than including the same code in all of them I could just pull that into a class they all derive from. After thinking more about it using a Mapper makes a lot more sense so I'll be changing my code to use that approach.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.