问题
I'm working on opencart Version 1.5.1.3. At one stage i want to use front side model function in the admin side controller file.
Can anyone please help me.? It would be appreciated.
回答1:
I know it's late but maybe useful for future works.
Simply you can add a function to your /system/engine/loader.php
. But you may know doing this directly may harm you in the future. So do it through vqmod
. I'll tell you how:
<?xml version="1.0" encoding="UTF-8"?>
<modification>
<id>Loadin Catalog Models</id>
<version>1.0</version>
<vqmver>2.X</vqmver>
<author>Hossein Shahsahebi</author>
<file name="system/engine/loader.php">
<operation info="Add function which I could access catalog models from admin">
<search position="after"><![CDATA[
protected $registry;
]]></search>
<add><![CDATA[
public function catalogModel($model) {
$file = DIR_CATALOG . '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();
}
}
]]></add>
</operation>
</file>
</modification>
You can put this code in file name your_own_chosen_name.xml
and place it in /vqmod/xml
.
Now for using e.g. shipping/flat
model of catalog directory in admin use this:
$this->load->catalogModel('shipping/flat');
回答2:
Put simply, you can't. What you need to do is either duplicate the model file if one of the same name doesn't exist in the admin side, or add the method you need to the admin side model file
回答3:
This is what I've done: You have a model catalog/model/foo/frontbar.php and another model admin/model/foo/adminbar.php
You want to inlcude frontbar.php in adminbar.php and then access frontbar's methods.
in adminbar.php do something like this:
<?php
include_once __DIR__.'/../../../catalog/model/foo/frontbar.php';
class ModelFooAdminbar extends Model {
private $frontInstance;
public function fromFront()
{
if(!$this->frontInstance){
$this->frontInstance = new ModelFooFrontbar($this->registry);
}
return $this->frontInstance;
}
}
?>
Then in your admin controller do something like this:
$this->load->model('foo/adminbar');
$this->data['someFrontData'] = $this->model_foo_adminbar->fromFront()->getSomeMethodInFrontbar();
来源:https://stackoverflow.com/questions/10800115/use-front-side-model-function-from-admin-side