Post by plugables » Mon Feb 02, 2015 8:46 pm

I have a question from Daniel and developers

What is it that prevents supporting loading of admin models from catalog and vice versa?
Are there any technical reasons for this?

Here is a previous related discussion about this topic.
http://forum.opencart.com/viewtopic.php?f=20&t=47856

My understanding is that in order to support this we just need the following updates
- Define two new variables in config
-- DIR_ADMIN
-- DIR_CATALOG
Currently we only have 'DIR_APPLICATION' which refers to the current context be it catalog or admin.

update the model function in system/engine/loader.php and add an optional parameter

Code: Select all

	public function model($model, $base='APPLICATION') {
		switch(strtoupper($base))
		{
			case 'ADMIN':
				$file = DIR_ADMIN . 'model/' . $model . '.php';
				break;
			case 'CATALOG':
				$file = DIR_CATALOG . 'model/' . $model . '.php';
				break;			
			default:
				$file = DIR_APPLICATION . 'model/' . $model . '.php';
		}
		
		$class = 'Model' . preg_replace('/[^a-zA-Z0-9]/', '', $model);

		if (file_exists($file)) {
			include_once($file);

			$this->registry->set('model_' . str_replace('/', '_', $model), new $class($this->registry));
		} else {
			trigger_error('Error: Could not load model ' . $file . '!');
			exit();
		}
	}
Shouldn't this be part of OC? Am I missing something. Is there some impact of loading modules from other context that I should know?

OpenCart Add-ons by Plugables

Image


New member

Posts

Joined
Thu Jan 01, 2015 8:07 pm

Post by plugables » Tue Feb 03, 2015 5:50 pm

I will answer this myself. I found this out after playing with the code.

Models in one 'context' i.e; admin or catalog may refer to other files in the same 'context'. Loading a model written for one context from the other will result in unexpected results or errors.

For example admin/model/sale/order when loaded will try to load in it another model 'marketing/affiliate'. If loaded from 'admin' it will work because there exists a 'marketing/affiliate' model in admin. However if loaded from catalog it will fail because it will try to load 'marketing/affiliate' in catalog context.

Since the code always implicitly assumes current application context, attempting to load models of one context from the other is not a good idea.

OpenCart Add-ons by Plugables

Image


New member

Posts

Joined
Thu Jan 01, 2015 8:07 pm
Who is online

Users browsing this forum: No registered users and 4 guests