Post by Aco » Wed Dec 07, 2011 4:02 am

Does anyone have any tips for loading an admin model (such as admin/model/setting/setting.php) from a front-end (catalog) model or controller?

I'm using the standard loader function at the moment:

Code: Select all

$this->load->model('setting/setting');
But this seems to limit you to only loading from *either* the catalog or admin folders depending on which one you're currently running your controller or model in. If this is the case, this seems like a real limitation of OpenCart, and makes a complete waste of existing models...

Thanks for any help in advance!...

Aco
Newbie

Posts

Joined
Fri Sep 09, 2011 2:08 pm
Location - UK

Post by justinv » Fri Dec 16, 2011 9:46 am

You are right, I suppose, there is quite a lot of duplication in code based on the admin and catalog being separate. It is nice though, because some functions are the same in admin and catalog, but have different results. For example, getProducts in admin returns all products so you can edit them, whereas in the frontend it gets them to display customers. Therefore one getProducts returns all products, the other only returns products with status = 1 (enabled).

Little differences like this make it nice to have them separate, and you can just copy the functions you need into the right file if you want to use them.

Documentation: OpenCart User Guide
Mods: Total Import PRO | CSV Import PRO | Ecom Tracking | Any Feed | Autosuggest | OpenCart CDN
Image


Active Member

Posts

Joined
Tue Oct 12, 2010 1:24 pm

Post by oasisfleeting » Thu May 31, 2012 8:44 am

All they would have to do is rewrite the loader.php file's model function to include a second optional argument which specifies front or back end. But since that doesn't exist the best way is to vqmod this function into your system/engine/loader.php file.

Code: Select all

	public function adminmodel($model) {
		
		$admin_dir = DIR_SYSTEM;
		$admin_dir = str_replace('system/','admin/',$admin_dir);
		$file = $admin_dir . 'model/' . $model . '.php';		
		//$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 ' . $model . '!');
			exit();					
		}
	}
but then of course any calls to other models within that model will be trying to call admin models as well so you would have to vqmod that as well.

This is terrible.

Newbie

Posts

Joined
Thu May 31, 2012 8:21 am

Post by gurubob » Wed Jul 04, 2012 8:51 am

Agreed, it is terrible. We did this in the catalog controller:

Code: Select all

// Extreme example to illustrate the danger ...
require('admin/model/sale/customer.php');
$adminCustomerModel = new ModelSaleCustomer( $this->registry );
$adminCustomerModel->deleteCustomer( 123 );
I don't recommend this - who knows what it will break, when it will break or what it will conflict with.

There is a question to be asked though which is "why should you be allowed to load an admin model from a catalog controller" - the catalog models are designed specifically to give the correct amount of access to the system that the catalog controllers require. By allowing admin models to be loaded by the catalog controllers the security model of opencart is compromised. Although awkward and inconvenient the physical separation and inability to load admin models from a catalog controller makes great sense. Spiderman's uncle was right ...

Effort should be directed into enabling the catalog models to provide the information you require.

User avatar
Newbie

Posts

Joined
Fri May 21, 2010 7:09 pm
Location - Dunedin, NZ

Post by sitesrus » Thu Aug 22, 2013 10:15 am

Has this been addressed yet? I can still do it your way, but I was hoping I could just easily load in the admin area stuff?

I have code I don't want to duplicate and I need access to order stuff, I'm running a simple front end cron and getting information for it so no need to duplicate code but I could write my own custom duplicate code if necessary.

Your way seems to work so I guess that's alright for now...

Newbie

Posts

Joined
Sun Aug 18, 2013 2:38 pm

Post by plugables » Sat Jan 31, 2015 6:57 pm

I think this is something that should be supported in OpenCart. Many a times you may need to run a piece of code or a function that has already been written in the admin area. There must be a way to reuse the same code. Duplication of the same code does not seem to be a good approach. It only increases the burden of maintenance.

OpenCart Add-ons by Plugables

Image


New member

Posts

Joined
Thu Jan 01, 2015 8:07 pm

Post by ksawl » Sat Apr 28, 2018 4:36 am

Hi, im use this code:

Code: Select all

        private function load_model($model) {
            require('catalog/model/'.$model.'.php');
            $model_path = 'model_'.str_replace('/', '_', $model);
            $model_name = $this->create_model_name($model);
            $this->$model_path = new $model_name( $this->registry );
        }
        private function create_model_name($model) {
            $m_expl = explode('/', $model);
            foreach($m_expl as $m) {
              $m_UC[] = ucfirst($m);
            }

            return 'Model'.implode('', $m_UC);
        }
        
model i cause so

Code: Select all

$this->load_model('account/customer');
 $customer = $this->model_account_customer->getCustomer(78);

Newbie

Posts

Joined
Thu Apr 12, 2018 7:35 pm

Post by dbdropper » Fri Dec 31, 2021 1:58 pm

justinv wrote:
Fri Dec 16, 2011 9:46 am
You are right, I suppose, there is quite a lot of duplication in code based on the admin and catalog being separate. It is nice though, because some functions are the same in admin and catalog, but have different results. For example, getProducts in admin returns all products so you can edit them, whereas in the frontend it gets them to display customers. Therefore one getProducts returns all products, the other only returns products with status = 1 (enabled).

Little differences like this make it nice to have them separate, and you can just copy the functions you need into the right file if you want to use them.
No, none of this is nice.

You should not duplicate a whole function when you can use just one, and have it accept an argument that tells it whether to return all products, or filter by a condition. In exchange, you not only make your code more maintainable, but also get a function that can be put to much more uses than you may have thought of.

As for just copy the functions, I hope in the 10 years that passed since you wrote the above, you have learned enough to see what's wrong with it

User avatar
New member

Posts

Joined
Mon Oct 24, 2016 4:42 pm
Who is online

Users browsing this forum: No registered users and 271 guests