Page 1 of 1

Create own library in the opencart. Problem with using DB

Posted: Thu Mar 17, 2016 11:20 pm
by kagan
Hi all!

When I tried to create my own library with using database queries, I've got an error that I couldn't use a connection to the database.

Code: Select all

Notice: Undefined property: ShippingDelivery::$db in J:\home\techno\www\system\library\shippingDelivery.php on line 44
Fatal error: Call to a member function query() on a non-object in J:\home\techno\www\system\library\shippingDelivery.php on line 44
Before using my own library I added further code to include it in my project in the file "my_site/admin/index.php":

Code: Select all

$mylibr = new ShippingDelivery();
$registry->set('mylibrary', $mylibr); // +
Maybe anyone faced with this problem, as I can't find answer on my question..

Re: Create own library in the opencart. Problem with using D

Posted: Tue Mar 22, 2016 2:21 am
by kagan
I've done it!

Code: Select all

  require_once(DIR_SYSTEM . 'library/shippingDelivery.php');
  $delivery_library = new ShippingDelivery($registry);
  $registry->set('shipping_delivery', $delivery_library);
Thus we can use others throwing varible $registry.

And in our class we can use DB and other components:

Code: Select all

class shippingDelivery {
	private $db;
	private $config;
	private $cache;
	private $data = array();
	
	public function __construct($registry) {
		$this->db = $registry->get('db');
		$this->config = $registry->get('config');
		$this->cache = $registry->get('cache');
	}
	// Code
}