Post by lolx » Tue Jan 17, 2017 2:13 pm

Hi there,

I am creating a script I want to run with a cron but I don't want to hook it up to my store as a plugin.

I would like to copy some of the functions from a model but they all use the database object ($this->db->query(...)

Is there a way I can use this object in a script that isn't connected to my store?


If so can you guide me regarding what files I needs to include?

Newbie

Posts

Joined
Sat Oct 22, 2011 1:26 pm

Post by mikeross » Thu Mar 09, 2017 4:19 pm

I'm no moderator, but here's my suggestion. Use PHP and it's PDO library to gain access to the MySQL database for this. As for what SQL queries to run, look in the catalog/model/catalog/product.php model class file as an example. I still don't know if there's a way to load some framework wrapper class that loads the config file and the database classes, and then loads this model class -- that's not documented well enough for me yet to know. However, at least I can look at that product.php file and know how OpenCart's database schema works so that I can do the proper SQL queries for what I need. If you struggled with the code a bit, you might be able to write your own wrapper class that loads the database config file, the database class file, and then the product model class. That would allow you to do queries without having to type so many SQL statements. Plus, as the model classes have new features and bug fixes, which might break your existing SQL queries, you can get around that problem because you won't be doing direct SQL in such a case.

Newbie

Posts

Joined
Thu Mar 09, 2017 10:50 am

Post by lolx » Thu Mar 09, 2017 11:36 pm

mikeross wrote:
Thu Mar 09, 2017 4:19 pm
I'm no moderator, but here's my suggestion. Use PHP and it's PDO library to gain access to the MySQL database for this. As for what SQL queries to run, look in the catalog/model/catalog/product.php model class file as an example. I still don't know if there's a way to load some framework wrapper class that loads the config file and the database classes, and then loads this model class -- that's not documented well enough for me yet to know. However, at least I can look at that product.php file and know how OpenCart's database schema works so that I can do the proper SQL queries for what I need. If you struggled with the code a bit, you might be able to write your own wrapper class that loads the database config file, the database class file, and then the product model class. That would allow you to do queries without having to type so many SQL statements. Plus, as the model classes have new features and bug fixes, which might break your existing SQL queries, you can get around that problem because you won't be doing direct SQL in such a case.
Hi Mike!

Thanks for replying. What I ended up doing is taking the database queries in the model and just removing the opencart model. This way I could use the same functions without OpenCart's built in database wrapper because I couldn't figure out how to load that.

Newbie

Posts

Joined
Sat Oct 22, 2011 1:26 pm

Post by straightlight » Thu Sep 28, 2017 11:08 pm

Another way would of simply been by using the Opencart registry in order to recall the database with an identical namespace.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by daveyoi » Fri Sep 29, 2017 12:12 am

You can do this, we did the same thing for a product import script.

The key thing is the autoloader found in startup.php

Code: Select all

function library($class) {
	$file = DIR_SYSTEM . 'library/' . str_replace('\\', '/', strtolower($class)) . '.php';

	if (is_file($file)) {
		include_once($file);
		return true;
	} else {
		return false;
	}
}

spl_autoload_register('library');
spl_autoload_extensions('.php');
This is just the same as including the DB class files you need manually but is cleaner and will handle all library files.

Once you have that function registered you can just create a $db property on your custom class and instantiate the DB object.

Code: Select all

class import
{
	public $db;

	public function __construct()
	{

		if (file_exists('../config.php')) {
			require_once('../config.php');
		}


		$this->db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
Now in my new import class I can do $this->db->query , $this->db->escape and all other DB driver based calls ala opencart models.

Image
Tristar Web Solutions


New member

Posts

Joined
Sun Oct 05, 2014 2:38 am
Who is online

Users browsing this forum: No registered users and 5 guests