[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[jfw] Instantiating models.

In my App I've abstracted out the model instantiating method in the AbstractAppController to 

protected function getModel($mName = 'Default', $shared = true, $forceNew = false)

However I'm having a concern what is the best way to instantiate models.

When checking framework app repos, I usually see something like this:

$model = new $modelClass($this->container->get('db'), $modelState);

This assumes that all models are instance of AbstractDatabaseModel so I decided to use DI's buildObject method to auto provide required services:

// Get or instantiate model
if ($container->exists($mClass))
= $container->get($mClass, $forceNew);
= $container->buildObject($mClass, $shared);

// Set model state
$state = $this->modelState;

if ($state instanceof Registry)

However In that case the model state is not available in the constructor.
Moreover, if an instance of same model already exists in DI, the state is overwritten.

There is another possibility to handle the dependencies: All my models could extend directly AbstractModel and be DatabaseAware, InputAware or WhateverAware like my Controllers are ContainerAware and LoggerAware.

What are your thoughts on this?

Framework source code: https://github.com/joomla/joomla-framework
Visit http://developer.joomla.org for more information about developing with Joomla!
You received this message because you are subscribed to the Google Groups "Joomla! Framework Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-framework+unsubscribe AT googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-framework.