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

Re: [jfw] Instantiating models.



I've more or less come to the conclusion there is no generic pattern
for models because they all end up being application specific. What I
do end up with is some sort of factory pattern like:

class MyFactory implements ContainerAware
{
  public function __construct(Container $c) { ... }

  public function getModel($name) { /* use the Container to build the model */ }
}

My models may use AbstractModel but they may also extend nothing.

I'm not sure if that answers your question though :)

Regards,
Andrew Eddie


On 19 March 2014 20:25, piotr_cz <pkonieczny AT hotmail.com> wrote:
> 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))
> {
>     $model = $container->get($mClass, $forceNew);
> }
> else
> {
>     $model = $container->buildObject($mClass, $shared);
> }
>
> // Set model state
> $state = $this->modelState;
>
> if ($state instanceof Registry)
> {
>     $model->setState($this->modelState);
> }
>
> 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.

-- 
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.